0

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.

Bob Smith
  • 1
  • 1
  • 2

3 Answers3

5

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.

Kaj
  • 10,862
  • 2
  • 33
  • 27
  • Im a huge newbie at java.. But im going to try and understand this.. Do i just make this a new function and then put it in the while loop that makes the animation? – Bob Smith May 19 '11 at 06:43
  • Oh, never mind I think I see it.. So I would put my loops inside that block of code where it says "Preform a task"... right? – Bob Smith May 19 '11 at 06:44
  • You should not have a while loop that does make the animation. You need to change the code so that it draws only one frame of the animation. The code above should then trigger drawing, and each drawing will draw the next frame. – Kaj May 19 '11 at 06:45
0

Maybe a simple sleep might be enough for you?

Thread.sleep(milliseconds);
styken
  • 74
  • 6
  • It's not likely that a sleep is the correct answer when dealing with animations. Making the EDT/AWT thread sleep freezes the UI, and the EDT/AWT thread will then be blocked from handling events. – Kaj May 19 '11 at 06:53
0

Change your public static void main(String[] args){ to public static void main(String[] args) throws InterruptedException { and inside that method type in Thread.sleep(milliseconds you want);

lockks
  • 67
  • 2
  • 9