I am working on a project and I read as much as possible about double buffering in java. I want to add a component or panel or something to my JFrame that contains a double buffered surface for drawing. I want to use hardware acceleration if possible, otherwise use a regular software renderer. My code looks like this:
public class JFrameGame extends Game { protected final JFrame frame; protected final GamePanel panel; protected Graphics2D g2; public class GamePanel extends JPanel { public GamePanel() { super(true); } @Override public void paintComponent(Graphics g) { g2 = (Graphics2D)g; g2.clearRect(0, 0, getWidth(), getHeight()); } } public JFrameGame() { super(); gameLoop = new FixedGameLoop(); frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel = new GamePanel(); panel.setIgnoreRepaint(true); frame.add(panel); panel.setVisible(true); frame.setVisible(true); } @Override protected void Draw() { panel.repaint();
I created an abstract game class and a game loop that calls Update and Draw. Now, if you see my comments, this is my main problem. Is there a way to get the graphics once instead of going through repaint and paintComponent and then assign a variable to each redraw? Also, is this hardware acceleration by default? If not, what should I do to speed up its hardware?
source share