我正在开发一个项目,我已经阅读了尽可能多的关于java的双缓冲。我想要做的是添加一个组件或面板或东西到我的JFrame包含双缓冲表面绘制。如果可能,我想使用硬件加速,否则使用常规软件渲染器。我的代码看起来像这样到目前为止:Java双缓冲
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(); // aquire the graphics - can I acquire the graphics another way?
super.Draw(); // draw components
// draw stuff here
// is the buffer automatically swapped?
}
@Override
public void run() {
super.run();
}
}
我创建了一个抽象游戏类和一个调用Update和Draw的游戏循环。现在,如果您看到我的意见,那是我主要关心的问题。有没有办法获得图形一次,而不是通过重绘和paintComponent,然后分配每个重绘变量?此外,该硬件是否默认加速?如果不是,我应该怎么做才能使硬件加速?
相关:http://stackoverflow.com/questions/2067255/bufferstrategy-vs-diy-double-buffering-in-jframe – finnw 2011-05-08 00:12:58