在Window中使用Graphics2D和BufferStrategy

时间:2013-10-16 15:47:47

标签: java swing graphics jframe bufferstrategy

我一直在寻找这个问题的答案,但很少有关于如何解决问题的信息。我想要做的是能够使用Graphics2D在窗口内完成我需要的所有图形。我对使用Graphics2D和BufferStrategy不是很宽容,因为我有大量的现有代码使用这些代码使用计算机GraphicsDevice创建一个全屏窗口。这是我做的一个测试,但有些东西我不知道。

public static void main(String[] args) {
    //Creates a frame and sets properties
    JFrame frame = new JFrame("FrameDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setResizable(true);
    frame.setVisible(true);
    frame.createBufferStrategy(2);

    //Gets Graphics2D from the bufferstrategy
    BufferStrategy s = frame.getBufferStrategy();
    Graphics2D g = (Graphics2D)s.getDrawGraphics();

    //Draws a background and a line for testing
    g.setColor(Color.GRAY);
    g.drawRect(0, 0, 500, 500);
    g.setColor(Color.BLACK);
    g.drawLine(50, 50, 200, 50);

    //Displays the graphics to the frame
    frame.update(g);
    g.dispose();
    s.show();
}

运行时,这只会创建一个空框架,该框架设置为正确的尺寸并且不会产生错误,但不会显示线条和背景。

我的猜测是问题源于更新帧的最后三行代码。我的困惑是如何在使用BufferStategy时显示Graphics2D组件...你还需要更新帧还是只需要显示BufferStategy?任何帮助将不胜感激,并提前感谢您。

1 个答案:

答案 0 :(得分:0)

使用http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferStrategy.html上的示例我构成了这个框架示例。

public class BufferedStrategyTest extends JFrame implements Runnable, WindowListener {

    private Thread graphicsThread;
    private boolean running = false;
    private BufferStrategy strategy;

    public BufferedStrategyTest() {
        super("FrameDemo");
        addWindowListener(this);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setSize(500, 500);
        setResizable(true);
        setVisible(true);

        createBufferStrategy(2);
        strategy = getBufferStrategy();

        running = true;
        graphicsThread = new Thread(this);
        graphicsThread.start();
    }

    public static void main(String[] args) {
        new BufferedStrategyTest();
    }

    public void addNotify() {
        super.addNotify();
        createBufferStrategy(2);
        strategy = getBufferStrategy();
    }

    @Override
    public void run() {
          // Main loop
        while (running) {
            // Prepare for rendering the next frame
            // ...

            // Render single frame
            do {
                // The following loop ensures that the contents of the drawing buffer
                // are consistent in case the underlying surface was recreated
                do {
                    // Get a new graphics context every time through the loop
                    // to make sure the strategy is validated
                    Graphics g = strategy.getDrawGraphics();

                    g.setColor(Color.GRAY);
                    g.drawRect(0, 0, 500, 500);
                    g.setColor(Color.BLACK);
                    g.drawLine(50, 50, 200, 50);

                    // Dispose the graphics
                    g.dispose();

                    // Repeat the rendering if the drawing buffer contents
                    // were restored
                } while (running && strategy.contentsRestored());

                // Display the buffer
                strategy.show();

                // Repeat the rendering if the drawing buffer was lost
            } while (running && strategy.contentsLost());
        }
        setVisible(false);
        dispose();
    }

    @Override
    public void windowActivated(WindowEvent e) {}
    @Override
    public void windowClosed(WindowEvent e) {}
    @Override
    public void windowClosing(WindowEvent e) {
        running = false;
    }
    @Override
    public void windowDeactivated(WindowEvent e) {}
    @Override
    public void windowDeiconified(WindowEvent e) {}
    @Override
    public void windowIconified(WindowEvent e) {}
    @Override
    public void windowOpened(WindowEvent e) {}
}