Declare these variables with your other declarations:
long framerate = 1000 / 60;
// time the frame began. Edit the second value (60) to change the prefered FPS (i.e. change to 50 for 50 fps)
long frameStart;
// number of frames counted this second
long frameCount = 0;
// time elapsed during one frame
long elapsedTime;
// accumulates elapsed time over multiple frames
long totalElapsedTime = 0;
// the actual calculated framerate reported
Now, when the program is preparing for a 'loop-around' of the code, insert this:
// calculate the time it took to render the frame
elapsedTime = System.currentTimeMillis() - frameStart;
// sync the framerate
try {
// make sure framerate milliseconds have passed this frame
if (elapsedTime < framerate) {
Thread.sleep(framerate - elapsedTime);
}
else {
// don't starve the garbage collector
Thread.sleep(5);
}
}
catch (InterruptedException e) {
break;
}
++frameCount;
totalElapsedTime += (System.currentTimeMillis() - frameStart);
if (totalElapsedTime > 1000) {
reportedFramerate = (long) ((double) frameCount
/ (double) totalElapsedTime * 1000.0);
// show the framerate in the applet status window
System.out.println("fps: " + reportedFramerate);
// repaint();
frameCount = 0;
totalElapsedTime = 0;
}
Hopefully, the comment code is enough for you to get the gist of what is going on in this. Let me know if you have issues understanding this. Cheers.