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();