Solved

Java animation with steady framerate

Posted on 2002-04-02
4
277 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
Comment Utility
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
Comment Utility
... 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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
countHi2 challenge 7 44
topping2 challenge 13 56
Where to store the queries for modification of table 4 25
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now