Solved

Java animation with steady framerate

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Title # Comments Views Activity
ejb stateless example 2 40
Is Applet the way to go for my drag and drop system? 8 34
hashmap order 17 40
What browser will run Java? 7 123
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

763 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