Java的双缓冲 [英] Java Double Buffering

查看:147
本文介绍了Java的双缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目,我已经读过了,就像我可以对在Java双缓冲。我想要做的就是添加包含双缓冲表面绘制到一个组件或面板或东西我的JFrame。我想,如果可以使用硬件加速,否则使用常规的软件渲染。我的code看起来像这样至今:

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();
    }
}

我创建了一个抽象的游戏类和游戏循环调用Update和Draw。现在,如果你看到我的意见,这是我关注的主要领域。有没有办法让图形一次,而不是通过重绘和持续的paintComponent,然后分配一个变量中的每个重绘?此外,这是硬件的默认加速?如果不是我应该怎么做,使之硬件加速?

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?

推荐答案

如果您希望在更新窗口,并充分利用硬件页面翻转(如果可用),您可以使用<一个更好地控制href=\"http://download.oracle.com/javase/6/docs/api/java/awt/image/BufferStrategy.html\"><$c$c>BufferStrategy类。

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:

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

    // draw stuff here

    bs.show(); // swap buffers
}

缺点是,这种方法并不能与事件驱动的渲染拌匀。通常你必须选择一个或其他。此外 getBufferStrategy 仅在实施帆布窗口使它与Swing组件不兼容。

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.

教程可以发现这里,的here 并的这里

Tutorials can be found here, here and here.

这篇关于Java的双缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆