• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Repainting Jpanel timer (60fps) 12% CPU usage

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 140
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic