Java Wait Function

佐手、 提交于 2019-12-04 06:22:38

问题


I was wondering if you guys could help me out. I'm trying to make an animation program with Java's built in graphics module... The thing is, Java executes everything at once; there isn't any time between the different animations. The end product is just the last picture. I need a function that puts like half a second in between each of the pictures.

Any help is appreciated.

Specs: Blue-J, JDK 6.

Edit: Btw, I'm a Java Newbie, and this is a class thing. The assignment was to make an animation, and press 'c' to go forward each frame, but I think thats kinda ghetto, so I want something better.


回答1:


Create a javax.swing.Timer that executes each X milliseconds, and draws one frame each time it is triggered.

This is the example from the javadoc:

  int delay = 1000; //milliseconds
  ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          //...Perform a task...
      }
  };
  new Timer(delay, taskPerformer).start();

Modify the delay, to e.g. 20ms. That will give you about 50 frames per second if your painting doesn't take too long.




回答2:


Maybe a simple sleep might be enough for you?

Thread.sleep(milliseconds);


来源:https://stackoverflow.com/questions/6054681/java-wait-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!