Correctly find the frame rate on a custom Java component

I am not sure that my framework code is correct, and I could not find exact examples of what I am looking for.

Essentially, I am subclassed java.awt.Component, and inside the method paint(Graphics)I call my function calculateFrameRate(), which is shown below. I do not do incremental drawing in update(). The numbers from this seem high, and I wonder if the internal double buffering of the Component class matches that the paint is called twice as much as it is displayed? I am rusty at double-buffer stuff, although this may be completely wrong.

Here is the frame rate method:

 private List<Long> updateTimes = new ArrayList<Long>();

private void calculateFrameRate() {
    long time = System.currentTimeMillis();

    updateTimes.add(new Long(time));

    // We will have the wrong framerate for the first 30 draws. No big.
    float timeInSec = (time - updateTimes.get(0)) / 1000f;

    currentFrameRate_ = 30f / timeInSec;

    if (updateTimes.size() == 31)
        updateTimes.remove(0);

}

Greetings

Hamy

+3
source share
1

System.nanoTime(). , FRAMES.

+1

Source: https://habr.com/ru/post/1775217/


All Articles