I am trying to make a string with FPS showing, but, how do I actually get my programs FPS? I want to do it with
g.drawString(getFPS(), 10, 10)
How do I get my FPS?
I am trying to make a string with FPS showing, but, how do I actually get my programs FPS? I want to do it with
g.drawString(getFPS(), 10, 10)
How do I get my FPS?
This code does it for me. Put it in your main program loop:
that call "gEngine.drawFPS" just tells my main draw method to include the fps at the top of the screen (and c is my Canvas) - you can use a similar method to give your "g" class the correct framesCountAvg data to draw
Although this question sounds a little bit vague, I'll still try my best and answer it. As the original post dates back to 2011 then OP will most likely not benefit from my answer. Hopefully it'll provide better footing for those who follow, though (the previous 2 answers seem a bit too messy and laconic).
Here's a code snippet that I've often used for collecting and displaying the frame rate of various Java GUI applications:
The obvious advantages of my approach (with regards to the previous solutions) are:
Graphics
orJLabel
amongst the calculations and you're good.javax.Swing.Timer
s or threads - callsubmitReading()
on the timer/thread whose performance you are trying to assess while runninggetFrameRate()
from another timer/thread to get the result (for example - you could write it to aJLabel
, file, etc).(PS.: Note that with Swing, all painting operations need to be completed on the Event Dispatcher Thread (EDT). Thus, no drawing work should be delegated to other (raw) threads without the use of special commands such as
SwingUtilities.invokeLater(..)
).Hope this helps!
Calculate the time elapsed and
1000/elapsedTime
will be your fps.