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
Solved

Java animation with steady framerate

Posted on 2002-04-02
4
287 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

838 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