Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problem with FPS Counter  (Read 2632 times)
0 Members and 1 Guest are viewing this topic.
Offline KONI

Junior Devvie

Java games rock!

« Posted 2004-12-03 07:21:16 »


In my main game loop, I am using this simple method to count the number of frames:

                 long startTime = System.currentTimeMillis();
                 usedTime = System.currentTimeMillis() - startTime;

where the functions are pretty self-explaining for a 2d shooter game. They contain all the world logic and render() just calls the repaint() method from my graphical interface.

The problem is that I keep getting extremely low usedTime's, +/- 1 msec. The problem is that it seems that a call to repaint() is pretty fast and that it doesn't take into account the actual time to do the paint().

So would you suggest to set usedTime() at the end of the paint() or is there a better way ?


I tried to move the usedTime calculation at the end of the paint() function and am getting better results. My framerate is now around 40 fps which seems ok. But I keep getting an odd problem:
I am using a thread with a timeout of 40 to run the main game loop and as described in the Java2D forum, I am trying to blur the foreground BufferedImage on the fly to make it look better.
This gives me a very rough animation and the smoothness of the animation (which I get without using the BLUR) is gone.
But my framecounter keeps telling me that I have 50 FPS which is impossible when I look at the actual result ?!
Offline oNyx

JGO Coder

Medals: 2

pixels! :x

« Reply #1 - Posted 2004-12-03 16:19:54 »

System.currentTimeMillis() is a rather bad timer. It's accuracy equals the Operating Sytem's tick rate. 50-55msec on win9x, 10msec on 2k/xp/mac and 1msec (or so) on linux.

If you're using 1.5 you can use System.nanoTime() instead - it's a hi res timer.

弾幕 ☆ @mahonnaiseblog
Offline Grazer

Junior Devvie

My other avatar is much more flattering.

« Reply #2 - Posted 2004-12-05 18:52:03 »


As far as I understand it, using repaint() is a really bad way to go for games. As you've probably realised by now, it simply schedules the targetted component for repainting the next time the Swing thread is free to do so, rather than actually repainting the GUI then and there.

The recommended way to do games is to turn off auto-repainting and force your own painting inside the render() method that you already have.

Chet's gaming articles should help:

Current Project: Easy Decal
Other Stuff: grlea online
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 69_Jeremy_69

Senior Newbie

Java games rock!

« Reply #3 - Posted 2004-12-07 20:38:49 »

Add this method.
    public void paintFps(Graphics2D g) {
          g.setFont( new Font("Arial",Font.BOLD,12));
          if (usedTime > 0){
              g.drawString(String.valueOf(1000000000/usedTime)+" fps",screenWidth-50,screenHeight-10);
              g.drawString("--- fps",screenWidth-50,screenHeight-10);

Then do what oNyx suggested.
long startTime = System.nanoTime();

usedTime = System.nanoTime()-startTime;

usedTime is a private global.

Call paintFps in your render loop.

Then follow Grazer's instructions, the componets should have all the setIgnoreRepaint(true);

And you should use a custom render loop.
Pages: [1]
  ignore  |  Print  

EgonOlsen (47 views)
2018-06-10 19:43:48

EgonOlsen (29 views)
2018-06-10 19:43:44

EgonOlsen (49 views)
2018-06-10 19:43:20

DesertCoockie (213 views)
2018-05-13 18:23:11

nelsongames (130 views)
2018-04-24 18:15:36

nelsongames (129 views)
2018-04-24 18:14:32

ivj94 (870 views)
2018-03-24 14:47:39

ivj94 (131 views)
2018-03-24 14:46:31

ivj94 (782 views)
2018-03-24 14:43:53

Solater (146 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!