Java 双缓冲
我正在做一个项目,并且我已经阅读了尽可能多的关于 java 中的双缓冲的内容。我想要做的是将一个组件或面板或其他东西添加到我的 JFrame 中,其中包含要绘制的双缓冲表面。如果可能的话,我想使用硬件加速,否则使用常规软件渲染器。到目前为止,我的代码如下所示:
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 的游戏循环。现在,如果你看到我的评论,那就是我主要关心的领域。有没有办法一次性获取图形,而不是通过 repaint 和 PaintComponent 然后在每次重绘时分配一个变量?另外,这个硬件默认加速吗?如果不是,我应该怎么做才能使其硬件加速?
I'm working on a project and I've read up as much as I can on double buffering in java. What I want to do is add a component or panel or something to my JFrame that contains the double buffered surface to draw to. I want to use hardware acceleration if possible, otherwise use regular software renderer. My code looks like this so far:
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();
}
}
I created an abstract game class and a game loop that calls Update and Draw. Now, if you see my comments, that's my main area of concern. Is there a way to get the graphics once instead of going through repaint and paintComponent and then assigning a variable every redraw? Also, is this hardware accelerated by default? If not what should I do to make it hardware accelerated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望更好地控制窗口更新时间并利用硬件页面翻转(如果可用),您可以使用
BufferStrategy
类。您的
Draw
方法将如下所示:缺点是这种方法不能与事件驱动渲染很好地混合。通常您必须选择其中之一。此外,
getBufferStrategy
仅在Canvas
和Window
中实现,使其与 Swing 组件不兼容。可以在此处找到教程,此处 和 此处。
If you want more control over when the window is updated and to take advantage of hardware page flipping (if available), you can use the
BufferStrategy
class.Your
Draw
method would then look something like this:The downside is that this approach does not mix well with event-driven rendering. You generally have to choose one or the other. Also
getBufferStrategy
is implemented only inCanvas
andWindow
making it incompatible with Swing components.Tutorials can be found here, here and here.
不要扩展
JPanel
。扩展JComponent
。它实际上是相同的,并且干扰代码更少。另外,您只能在paintComponent
中执行绘图代码。如果您需要手动刷新组件,您可以使用component.redraw(
)。Don't extend
JPanel
. ExtendJComponent
. It's virtually the same and has less interfering code. Also, you'd do the drawing code inpaintComponent
only. If you need to manually refresh the component, you'd usecomponent.redraw(
).