I'm building a simple 2d java game and having some flickering issues even though I draw the graphics using a double buffering technique.
I've just got started and have a very simple game loop this far. But I think it should work fine anyway? Without any flickering.. Here's how I do it:
@Override
public void run() {
while (running) {
currentState.update();
prepareGameImage();
currentState.render(gameImage.getGraphics());
repaint();
try {
Thread.sleep(14);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.exit(0);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (gameImage == null) {
return;
}
g.drawImage(gameImage, 0, 0, null);
}
private void prepareGameImage() {
if (gameImage == null) {
gameImage = createImage(gameWidth, gameHeight);
}
Graphics g = gameImage.getGraphics();
g.clearRect(0, 0, gameWidth, gameHeight);
}
You can see how I mean in this video
I'm looking for any tips that could lead me to whats causing this glitch. Am I doing double buffering wrong?
Any help appreciated!
Thanks