This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Repainting Jpanel timer (60fps) 12% CPU usage  RSS feed

 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I had a thread in another forum but its too long to read so... This ones the extension of the same project I've been working on. I've figured out that lots of the lagginess was coming from the lack of synchronisation and the bad allocation of elements so I corrected that and now the physics code part is a lot better, and I tested the program with and without the JPanel.repaint() and found an increase of 8% CPU to 20% CPU.

The code for repainting goes like this:

 
Les Morgan
Rancher
Posts: 776
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are saying is not normal.  First off you need to make your program event driven, here is a thread that, I believe, does a decent job of telling how to do event driven. Use a Swing Timer to make your animation loop, it will get rid of your wasted overhead of trying to control your application instead of letting Java do what it was made to do.

Swing Timers also run in a single thread so you don't have overhead eaten up with multiple timers.

I've literally had thousands of objects on a screen and not consumed the resources you are asking about.
 
Phil Freihofner
Ranch Hand
Posts: 128
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with the advice to make things more event driven. But I think the util.Timer is usually a better bet for games than the Swing.Timer. It is true that you need to be more careful with interacting with Swing components, but this can often be handled by posting code on the EDT directly when necessary. Too much activity on the EDT really slows down performance--it quickly becomes a significant bottleneck.

In "Killer Game Programming" there are some test cases with the game loop timing being provided via a util.Timer vs a Swing.Timer, and the util.Timer ended up with much better performance, pretty much identical in performance to the fastest game loop structure that the book described and preferred.

Even easier to code, though, is the AnimationTimer in JavaFX! It runs at 60fps as a default.

Instead of BufferedImages, you would be using WritableImages. There is no need for either repaints or paint() methods or dealing Graphics2D and things like opacity are a simple property setting. There is a tutorial over at JavaGaming.org if you want a quick intro to JavaFX game loops. I've started converting a game I starting with Java2D to JavaFX and am really pleased with how much easier and cleaner the JavaFX code is. And, it integrates well with functional programming constructs that have been added in Java 8.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!