0
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Take a look at BufferStrategy, it's the preferred way to use multibuffering for java 2d, - and it works like a charm. Searching google for examples on how to use it should be straightforward :)

\$\endgroup\$
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.