Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Game Loop, calculate the Frames Per Second.

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Thanked 0 Times in 0 Posts

    Default Game Loop, calculate the Frames Per Second.

    Ok, so first of all I would like to say that I'm still very 'Noob-ish' when it comes to programming in Java.

    I'm creating a simple game, however I thought it would be a nice challenge to display the frames per second in my game.
    But all the articles about it are really complex (for me) , and nobody seems to have a game loop the way I do.
    This is what my game loop looks like:

    My Main class extends the JPanel
     public class Main extends JPanel {

    Also in my main class the game loop, it uses the default repaint() method. In wich I draw all the images etc.
    public void run() {
      while (true) {
        long time = System.currentTimeMillis();
        time = (1000 / Globals.maxFPS) - (System.currentTimeMillis() - time);
        if (time > 0) {
          try {
          } catch (Exception e) }

    My first thought was to calculate the FPS using the time variable, however no mather how slow the game goes time is always 16. So I realy don't understand how I should calculate the FPS in this game loop.

    If you need any more information I'll be happy to give it

    Thanks in Advance,

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: Game Loop, calculate the Frames Per Second.

    There are a couple ways to do this.

    Each frame, calculate the time (in seconds) it has been since the last frame. Divide 1 by that number, and that's your momentary fps.

    Do a similar calculation, only spread out over more time- every x number of frames, calculate how long it's been since the last x frames. Take x divided by that time, and that's your fps.

    Or do it the other way around- every second, calculate how many frames have passed. That's your fps. Or every 5 seconds, divided by 5.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Game Loop, calculate the Frames Per Second.

    it uses the default repaint() method. In wich I draw all the images etc.
    Do you have a repaint() method with your code in it? That is not normal and I don't know how it would work.
    The call to the repaint() method will return without much delay.
    (System.currentTimeMillis() - time) will be 0 most of the time.

    I'm not sure I understand your problem. Where is the FPS value shown?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. while loop in GUI game
    By Hamran2011 in forum Loops & Control Statements
    Replies: 6
    Last Post: November 4th, 2011, 10:04 AM
  2. [SOLVED] Need Loop Help for Game
    By Shivam24 in forum Loops & Control Statements
    Replies: 9
    Last Post: July 18th, 2011, 07:41 PM
  3. [SOLVED] Using for loop to calculate sine function (factorial issue)
    By Actinistia in forum Loops & Control Statements
    Replies: 2
    Last Post: March 11th, 2011, 03:38 PM
  4. GIF frames
    By vsector in forum AWT / Java Swing
    Replies: 0
    Last Post: April 15th, 2010, 05:25 PM
  5. A thread as game loop
    By maikeru in forum Threads
    Replies: 0
    Last Post: December 25th, 2009, 09:01 PM