1 Reply Latest reply: Dec 31, 2010 3:15 AM by gimbal2 RSS

    Calculating FPS using a Thread and Graphics2D object to render

    827681
      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();
                
                // 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)
                          sleepTime = 0;
                     
                     try
                     {
                          Thread.sleep(sleepTime);
                     }
                     catch(InterruptedException ie)
                     {
                          System.err.println("Error: Thread exception!");
                     }
                     beforeTime = System.currentTimeMillis();
                }
                
                System.exit(0);     // end of run
           }


      I was wondering how I could calculate the FPS I am rendering so that I can output it at runtime.