I have a game which is painting a lot of gifs and images everywhere and it runs a little bit slow meanwhile. Sometimes it can run very good for a minute or something, but then it starts to lag and stuff. Most of the images flicker from time to time. Not extremely much, but they still flicker a little bit.
I will shoe the relevant parts of my code and maybe you can find what I need to do. I have googled and seen things with dubble buffering and stuff, but I don't really get it.
The code here is just a lot of stuff, I have removed a lot of "game logic" and image files so the names are not the same, but you will at least get my method with this code:
public class Game3 extends JPanel implements ActionListener{
ImageIcon flowerIcon = new ImageIcon("img\\Dragon.gif");
Image flower = flowerIcon.getImage();
ImageIcon flowerhitIcon = new ImageIcon("img\\DragonHit.gif");
Image flowerhit = flowerhitIcon.getImage();
ImageIcon flowerfireIcon = new ImageIcon("img\\DragonFire.gif");
Image flowerfire = flowerfireIcon.getImage();
URL music = null;
AudioClip musicSound;
URL pop = null;
AudioClip popSound;
Font font;
Random r;
boolean skott = false;
Nalle nalle = new Nalle();
ArrayList<Bubble> bubblor = new ArrayList<Bubble>();
Timer tim;
Are you regulating the FPS (Frame per seconds) of your game loop ? You do not need to draw constantly the images because it will just provoke a huge lag. You can simply draw your images let's say 25 times per seconds. I don't think you should override the PaintComponent method to draw all your images as you can't control when this method is called.
I think you should try going with Slick2D API, it provides everything you need for your game and it's pretty simple to use. It's not a good idea to use Swing as you do for a game as Swing is slow and is not made for games, it's made for interface such as menus.
Slick2D will provide you a game engine which will regulate the FPS and a Graphic object where you can draw your images.
Normally, you will repaint them but only a fixed number of times in a second. This way you can control the speed of the game. But first, I think you really shouldn't use Swing to make a game, you will get other performances issues like the one you have.
As you can see it's pretty much the same concept that you are using for drawing, but the API provides a game container which is a game loop in a independant window.
Well, in my case then I rely on the timer which is 20 frames per millisecond. If I make a separate update mechanism, wouldn't like 100 or something be a little bit too slow?
My game is very soon finished and it works fine as it is at the moment, but not perfect - that's why I made this thread. I will try the SLIK on my next game!
Where panel is my extended JPanel with the overriden paintComponent() method (I saw that I'm not calling repaint() at the end of my paintComponent() method, which supports PsychotikRabbit's suggestion).
Where frame is a JFrame.
It's been a while since I last did graphics in native Java, but from the looks of it, you're not using active rendering. This article helped me a lot, when I got tired of the slow repaint() calls: http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-active-rendering-r2418