0

我一直在寻找这个问题的答案,但是关于如何解决这个问题的信息很少。我想做的是能够在一个窗口中使用 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?任何帮助将不胜感激,并提前感谢您。

4

1 回答 1

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) {}
}
于 2013-10-16T16:16:44.810 回答