Solved

Java animation with steady framerate

Posted on 2002-04-02
4
286 Views
Last Modified: 2010-03-31
Do You have an example how to make dobbelbuffering animation with the same FPS on every machine?
If the machine is to slow it should stop, but if it is fast enough it should show 25 frames per seconds.

I have tried it but it starts flickering, when i change the thread sleep pause at each frame.

There must be one right way to do it.
0
Comment
Question by:stigc
  • 2
4 Comments
 
LVL 9

Expert Comment

by:Ovi
ID: 6915021
DoubleBuffering looks like :

class test extends Canvas {
  private Image debug;
  private Grapgics debugG;

  public void update(Graphics g) { paint(g); }

  public void paint(Graphics g) {
    int w = getSize().width;
    int h = getSize().height;
    if(debug == null || debug.getWidth(this) != w || debug.getHeight(this) != h) {
       debug = createImage(w, h);
       debugG = debug.getGraphics();
    }
    // do your paint here but not using g graphics object but debugG, like :
    debugG.setColor(Color.white);
    debugG.fillRect(0, 0, w, h);
..................................
    g.drawImage(debug, 0, 0, this);
  }
}
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6915027
... and now your thread should not produce flickering if you adjust the delay time. Also be carefull to eliminate any time consumming operation including System.out calls, because you will have then a non countinous animation.
0
 

Author Comment

by:stigc
ID: 6915461
dubbelt buffering was not the issue, but the framerate.
I know how to make dobbeltbuffering.
0
 
LVL 2

Accepted Solution

by:
triso earned 200 total points
ID: 6932269
Well, stigc, this should work for you:


  long start = System.currentTimeMillis();
  draw();
  long duration = System.currentTimeMillis() - start;

  long waitingTime = 1000 / 25 - duration;

  if (waitingTime > 0) {
    sleep(waitingTime);
  }

If waitingTime is < 0 then you could stop since the frame rate has dropped below 25.

Watch out on some PCs because the clock may be accurate to 1/18 second only.


0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question