I was wondering if anyone could show me any code where they have used a JPanel
with double buffering and active rendering using a thread and was able to accurately and effectively calculate and display their FPS.
Here is my code:
/**
* Implements the Runnable interface. Responsible
* for updates, rendering, and sleeping the thread.
*/
@Override
public void run()
{
bRunning = true; // Keep flag set
long beforeTime, deltaTime, sleepTime;
beforeTime = System.currentTimeMillis();
initRender();
// Repeat update/animation loop, sleeping a bit
while (bRunning)
{
try
{
updateBoard();
} catch (IOException e)
{ System.err.println("Error: IO Failure"); }
renderBoard();
paintBoard();
deltaTime = System.currentTimeMillis() - beforeTime;
sleepTime = PERIOD - deltaTime;
if (sleepTime < 0)
{
timeOver = (-1) * (int) sleepTime;
sleepTime = 0;
}
try
{
Thread.sleep(sleepTime);
}
catch(InterruptedException ie)
{
System.err.println("Error: Thread exception!");
}
beforeTime = System.currentTimeMillis();
}
System.exit(0); // end of run
}
/**
* Renders the resulting output frame to
* an Image buffer. Off-screen rendering.
*/
private void renderBoard()
{
// Draw background, using correct coordinates
gDevice.drawImage(background, bX[0], 0, null);
gDevice.drawImage(background, bX[1], 0, null);
gDevice.drawImage(background, bX[2], 0, null);
gDevice.drawImage(background, bX[3], 0, null);
// Draw characters and all sprites
gChar.draw(gDevice);
// Test debug flags and take explicit action here
if (bFPSDebug)
calcAndDisplayFPS();
}
/**
* Used to calculate and paint the FPS on the GameBoard.
*/
private void calcAndDisplayFPS()
{
FPS = 1000 / ((Double) PERIOD + timeOver);
gDevice.drawString("FPS: " + String.valueOf(FPS), 20, 20);
}
This doesn’t seem to work as my output is constantly either 62.0 or 66.0. This is with a const PERIOD of 10 milliseconds. This means as long as my computer can handle it I should be sleeping for 10 milliseconds which should give me 1000/10 = 100 FPS cap. Apparently I am not achieving this.
Does anyone know if what I am doing is sufficient to get an accurate FPS count. And also is calculating and drawing to the screen every loop like this going to be a performance hit in itself?
If the