Recording FPS help

hi their I’ve been lurking on these forums for the last week or so and today I decided to make an account after being stuck with these issues (yay for leeching). I’m trying to make a game with java2d and I’m having issues recording the fps.

//variables used for finding fps
    private long prevStatsTime;
    double fps = 0;
    int frameCount = 0;
    long totalElapsedTime = 0L;

    public void run(){
        .....
        prevStatsTime = System.nanoTime();
        while(running = true){
             .....
             beforeTime = System.nanoTime();    
        }
        ......
        recordInfo();
    }

    public void recordInfo()
    {
         long realElapsedTime = System.nanoTime() - prevStatsTime;


        frameCount++;
        totalElapsedTime += realElapsedTime;
        if(totalElapsedTime > 0)
        fps = (((double)frameCount/totalElapsedTime)*1000000000L);
        prevStatsTime = System.nanoTime();
    }

    public void drawInfo(Graphics g)
    {
         ...
        g.drawString("FPS: " + fps, 0, 30);
    }


the issue I’m having is that the fps slowly decreases so obviously its not recording the fps >.< any help would be really great I would prefer a nudge in the right direction rather then you flat out explaining it to me if you wouldn’t mind.

many thanks


final long measureInterval = 1000 * 1000000L; // one second
long lastMeasure = System.nanoTime();
int frameCounter;
int lastFrameCount;

public void onRenderFrame()
{
    this.frameCounter++;

    long now = System.nanoTime();
    if(now >= this.lastMeasure + measureInterval)
    {
       this.lastFrameRate = this.frameCounter;
       this.frameCounter = 0;
       // WRONG: this.lastMeasure = now; 
      this.lastMeasure += measureInterval;
    }
}

thank you very much <3

yoink

Thanks :wink: