Solved

Java animation with steady framerate

Posted on 2002-04-02
4
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

752 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