basics of game development – designing the Framerate class

Welcome to this Java fundamental game series. In the last, we've made a
simple blank GUI using Swing's JFrame class Now we'll move ahead to design
fps meter which can provide the fps count to the game screen and we will
create the first java game example application ShowFPS which shows
framerate per second of the application.

Creating the FPS Counter: FrameRate class

The Fps meter is implemented through a FrameRate class, I have put it in
jgame.util package.

It contains 4 private fields and 3 methods.  

fps contains the string that will be drawn to the screen so I have created one getter to fps. 

start method will only be called for the first time to start the fps counter. 

calculate method will be invoked for each new frame.

Code for FrameRate class

Testing the FrameRate class

Only to test the frameRate class I made a new class called Test. 

Use a while loop to generate frames. While the game is running while loop should also be running. 

Since the Test class is used only to test whether or not the fps meter is working perfectly. 

I haven't made any condition to stop the game, We can stop it through the
ide though.

See or download the code for the Test class.

    

Run the app and see whether or not the fps counter is working fine.

Gaming window with framerate

java games example

I have made a new class ShowFPS, which includes the FrameRate object to measure the fps.

Other than clearing and redrawing the frame, nothing else is rendered in the window it is too basic and only shows you fps. also, it is an example of passive rendering. 

ShowFps located in the jgame.example package extends the JFrame class to inherit the methods capable of forming a window.

Because the swing library is not thread-safe so it is essential to create and show a JFrame on swing's EDT ( Event dispatch thread). 

However, the application's main method is not invoked on the EDT so we can use SwingUtilities.invokeLater() to launch the game Window from EDT. 

It is important to follow Java’s guidelines for threading when using Swing components for rendering because ignoring them can cause undefined behaviour, which is very difficult to debug.

See Concurrency in Swing

//we should create and show gui on event thread
    
final ShowFps app = new ShowFps();

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            app.createAndShowGUI();
        }
    });


JPanel is added to the window on which we can draw graphics by overriding its paint method. 

The paint method provides a Graphics object with this we can draw text to the window just by calling the Graphics.drawString("String text", x, y
coordinates). 

render method draws and schedules the next repaint event with a call to the
repaint() and it happens again and again, because of this, the application
redraws itself. 

Share this Post

Leave a Reply

Your email address will not be published. Required fields are marked *