I'm trying to make a little breakout game with Java and Swing. The only issue so far is that drawing on the JPanel causes lots of flickering!
The relevant code:
JFrame frame = new JFrame("Breakout");
JPanel gameField = (JPanel) frame.getContentPane();
private void gameLoop() {
while(running) {
update();
render((Graphics2D) gameField.getGraphics());
}
}
private void render(Graphics2D g) {
g.clearRect(0, 0, gameField.getWidth(), gameField.getHeight());
for (GameObject o : objects) {
o.draw(g);
}
}
I've looked into manually double buffering (e.g. by making an offscreen Image, drawing on it, then doing g.drawImage(offscreen). Is there a better way?