0

I'm making a relatively simple 2D space game, by using repaint() and then overloading paintComponent(Graphics g). For my game, there are many different images overlayed on each other, like a black background, multiple "space station" images, the player image, thruster effects, a HUD, "minimap"/radar, icons, dust effects, and so on. These are all BufferedImages and are painted onto the scene with g.drawImage(). However, now that I have about 15 different images being drawn on, the performance is quite bad on laptops/low powered computers. My estimate is about 10-15 frames per second.

I have a few different threads: one thread does the repaint(), one thread takes care of collision/physics/control, other threads coordinate the effects (like a cool stardust effect).

I wouldn't think that such simple 2D graphics would lag a machine that hard. For example, I am typing this on a laptop with an Intel i5 at 2.5 GHz, and getting about 20 fps on the game.

I searched around Stack Overflow for answers, but I couldn't find anything.

Also, what's interesting is that when the game transitions to a different scene, all the threads should end (while loops exit because of a Boolean variable) and the method that draws all the images is no longer called. However, when the next scene runs and only has about 3 different drawImage() calls, the bad performance remains.

I would put code, but the programming I've done so far is very scattered and wouldn't make much sense. Is there any common errors when using paintComponent that cause very bad performance?

Thanks.

EDIT: Here is my rendering thread:

Thread renderer = new Thread() {
        public void run() {
            while (true) {
                mainScreen.repaint();

            }
        }
};

renderer.start();
Zee2
  • 163
  • 8
  • You should one thread, that updates the state of the game play and the tells Swing to renderer, waiting the required amount of time in order to maintain the frame rate. Considering it's possible to [animate more the 4500 component based objects](http://stackoverflow.com/questions/14886232/swing-animation-running-extremely-slow/14902184#14902184), I doubt you should have to many problems...Without an example code, it's really impossible to know what's going wrong, but it sounds like you are spamming the EDT – MadProgrammer Feb 26 '14 at 04:05
  • I used to do that, but then it bound the speed of the physics and effects and animations to the framerate. With one thread `repaint`ing and other threads dealing with animation/physics/control the framerate does not matter. What do you mean by "spamming the EDT"? – Zee2 Feb 26 '14 at 04:11
  • You're not giving the Event Dispatching Thread time to breath so that it can physically process what needs to be update. The problem with multiple threads is how do you actually synchronize all the content? Everything needs to be staged so it can painted, this is the bottle neck in the system, so the screen shouldn't be able to update until everything that needs to be painted in the next cycle is ready... – MadProgrammer Feb 26 '14 at 04:14
  • So if I put the physics/control/animation/whatever in the same thread as the `repaint()` call, it should fix everything? Hmmm.... – Zee2 Feb 26 '14 at 04:16
  • I don't know about "fix everything", but it should cause less problems. You will also want to put in some kind of delay between updates in order to force a frame rate, [for example](http://stackoverflow.com/questions/21817745/java-w-slick-my-jump-animation-seems-to-jump-as-fast-as-the-processor-can-han/21817991#21817991) and [example](http://stackoverflow.com/questions/21921344/javagame-character-is-not-moving-accurately-while-drawing/21922123#21922123) – MadProgrammer Feb 26 '14 at 04:17
  • OK. I'll throw in a `Thread.sleep(10)` and see what happens. – Zee2 Feb 26 '14 at 04:19
  • But see, that's exactly how I had done it before. I had the bad performance before as well. I'll still redo it, but I'm not sure that's the answer... – Zee2 Feb 26 '14 at 04:23
  • Then, there's likely something else going on that effecting your performance... – MadProgrammer Feb 26 '14 at 04:25
  • I saw in another thread something about the BufferedImages not being the same "color model" as the display or something and impacting performance. Is that a possibility? – Zee2 Feb 26 '14 at 04:26
  • Yes, weather it's effecting you I don't know, but you could take a look at [this example](http://stackoverflow.com/questions/19486459/slow-java2d-drawing-on-integrated-graphics/19486532#19486532) – MadProgrammer Feb 26 '14 at 04:28

0 Answers0