简体   繁体   English

Java双缓冲

[英]Java Double Buffering

I'm working on a project and I've read up as much as I can on double buffering in java. 我正在研究一个项目,我已经尽可能多地阅读了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. 我想要做的是添加一个组件或面板或其他东西到我的JFrame,其中包含要绘制的双缓冲表面。 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. 我创建了一个抽象游戏类和一个调用Update和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? 有没有办法获取图形一次,而不是通过重绘和paintComponent,然后每次重绘分配一个变量? Also, is this hardware accelerated by default? 此外,此硬件是否默认加速? If not what should I do to make it hardware accelerated? 如果不是我该怎么做才能使硬件加速?

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. 如果您想要更好地控制窗口何时更新并利用硬件页面翻转(如果可用),则可以使用BufferStrategy类。

Your Draw method would then look something like this: 你的Draw方法看起来像这样:

@Override
protected void Draw() {
    BufferStrategy bs = getBufferStrategy();
    Graphics g = bs.getDrawGraphics(); // acquire the graphics

    // draw stuff here

    bs.show(); // swap buffers
}

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 in Canvas and Window making it incompatible with Swing components. 此外, getBufferStrategy仅在CanvasWindow实现,使其与Swing组件不兼容。

Tutorials can be found here , here and here . 教程可以在这里这里这里找到

Don't extend JPanel . 不要扩展JPanel Extend JComponent . 扩展JComponent It's virtually the same and has less interfering code. 它实际上是相同的,并且具有较少的干扰代码。 Also, you'd do the drawing code in paintComponent only. 此外,您只在paintComponent执行绘图代码。 If you need to manually refresh the component, you'd use component.redraw( ). 如果需要手动刷新组件,则使用component.redraw( )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM