Setup for games animation: How do I know JFrame is finished setting itself up?
- by Jokkel
I'm using javax.swing.JFrame to draw game animations using double buffer strategy.
First, I set up the frame.
JFrame frame = new JFrame();
frame.setVisible(true);
Now, I draw an object (let it be a circle, doesn't matter) like this.
frame.createBufferStrategy(2);
bufferStrategy = frame.getBufferStrategy();
Graphics g = bufferStrategy.getDrawGraphics();
circle.draw(g);
bufferStrategy.show();
The problem is that the frame is not always fully set-up when the drawing takes place.
Seems like JFrame needs up to three steps in resizing itself, until it reaches it's final size. That makes the drawing slide out of frame or hinders it to appear completely from time to time.
I already managed to delay things using SwingUtilities.invokeLater(). While this improved the result, there are still times when the drawing slides away / looks prematurely draw.
Any idea / strategy? Thanks in advance.
EDIT: Ok thanks so far. I didn't mention that I write a little Pong game in the first place. Sorry for the confusion What I actually looked for was the right setup for accelerated game animations done in Java. While reading through the suggestions I found my question answered (though indirectly) here and this example made things clear for me.
A resume for this might be that for animating game graphics in Java, the first step is to get rid of the GUI logic overhead.