View Full Version : Animation that is not dependent on fps?

12-19-2011, 05:48 AM
I've looked for information on how to set-up an animation in java. Everything I find shows how to set up a thread that implements Runnable, uses a while loop, the sleep method, and System.currentTimeMillis() in order to create a steady frame rate.

public void start() {
animator = new Thread(this);
animator.start(); // calls run

/* handles frame rate */
public void run() {
long tm = System.currentTimeMillis(); // stores what time the program started
while (Thread.currentThread() == animator) {
repaint(); // calls update
try {
tm = tm + delay; // tm + delay is when the next update should be.
Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
// if tm > currentTime, then sleep the difference.
// if program starts to lag, then don't sleep, update immediately!
} catch (Exception e) {
frame++; // everything drawn depends on what frame it is.

public void stop() {
animator = null;

The problem with this is that the animation is dependent on what frame it is. However, I have played java games wherein the frame rate might be very high (say over 100 fps, depending on your computer's specs), yet the animations do not play faster even if the frame rate is high.

And vice versa, the animation is not slowed down if the frame rate is going slow. In this case the animation may be choppy, but it seems like certain parts of the animation are being skipped over in order to have the animation finish within the normal amount of time.

What is this called? And how is it done?