5

I have created my own canvas that has been extended from the JPanel, however I have noticed that w/ the content and so forth, that all of the sudden my FPS took a hit. I am following the swing rules from Filthy Rich Clients, by using paintComponent, creating a clip area, only redrawing what has been changed, and so forth. I have the FPS set to a constant 50 FPS, and I notice that sometimes my FPS will jump down to 31/32 FPS and go back up to 50 and so forth. While running my program it's only using about 25MB of RAM and 0 of my CPU, even when rendering. I also have OpenGL set.

Note: I have NO images, this is strictly using the shapes in Graphics.

Is there a major performance hit by drawing everything on a JPanel? Should I be extending a different component (I keep seeing Canvas component)? How "smart" is it to build a game such as tetris (or any of the other retro games) in JPanel?

It's a possibility that this is a timer issue, as I just added 100 additional painting calls and the FPS still does the 50 32/31 thing.

abc123
  • 71
  • 3
  • 3
    After much investigation I have found that the issue is not the JPanel at all. As a matter of fact, the issue is with the Timer in java. It's not 100% accurate, which has resulted in the FPS being way off. My solution to fixing this was reading this: http://www.koonsolo.com/news/dewitters-gameloop/ – abc123 Apr 19 '11 at 04:07

2 Answers2

1

After much investigation I have found that the issue is not the JPanel at all. As a matter of fact, the issue is with the Timer in java. It's not 100% accurate, which has resulted in the FPS being way off. My solution to fixing this was reading this: koonsolo.com/news/dewitters-gameloop

abc123
  • 11
  • 1
  • Accuracy of the Timer is contingent upon the granularity of the OS clock interrupt implementation. WinXP for example only has about 15msec accuracy. Interesting dodge, though, is to run a background Thread.sleep(Long.MAX_VALUE); For some reason, this makes WinXP run with an accuracy of 1msec! http://www.java-gaming.org/index.php/topic,24311.msg206914.html#msg206914 – Phil Freihofner Aug 24 '11 at 19:55
0

I realise that you've found an issue with the Timer class, however I have another comment for you, which you may find useful:

You've not mentioned whether you're using the double-buffer technique. If you're not, then you may notice a FPS improvement.

Just in case you're not familiar with the technique, it involves creating a separate panel buffer, redraw your scene on this buffer, then switch this buffer with the one on the screen, etc.

Crollster
  • 2,751
  • 2
  • 23
  • 34