Java Wait Function

前端 未结 2 500
情深已故
情深已故 2021-01-28 12:55

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;

相关标签:
2条回答
  • 2021-01-28 13:14

    Maybe a simple sleep might be enough for you?

    Thread.sleep(milliseconds);
    
    0 讨论(0)
  • 2021-01-28 13:30

    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.

    0 讨论(0)
提交回复
热议问题