1

我现在编写了一个简单的RTS游戏,这是我在游戏设计方面的第一次体验。我的问题是,当我使用 createBufferStrategy(2) 时,在 bufferStrategy.show(); 之后未显示所有摆动元素(按钮等);方法被调用。我的游戏充满了按钮、表格和其他垃圾,我真的不想自己编写所有这些代码。我也很喜欢 Java 的布局,并希望在此基础上制作所有的 GUI。所以,这是一个小代码示例,不是来自我的游戏,但它很好地展示了我的问题。谢谢。顺便说一句,我了解我的问题的根源。我知道挥杆抽签机制是基于事件的,而使用缓冲策略不是基于事件的。但我不知道如何解决这个问题。谢谢你。最后 - 我不想使用默认的基于摇摆事件的方法,因为它对游戏来说很慢,据我所知,bufferstratgey 只是游戏的方法。谢谢。

public static void main(String[] args){
    final JFrame frame = new JFrame();
    JPanel menuPanel = new JPanel();
    final Button button = new Button("Exit");
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    menuPanel.add(button);
    frame.add(menuPanel);
    frame.setPreferredSize(new DimensionUIResource(800, 600));

    //frame.setResizable(false);
    //frame.setUndecorated(true);
    //frame.setIgnoreRepaint(true);

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            final long delay = 1000/60;
            frame.pack();
            frame.setVisible(true);
            final GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
            if (device.isFullScreenSupported()){
                device.setFullScreenWindow(frame);
                // this place. If I turn into fullscreen mode button disappear. And if I stay in windowed mode button exist and work;
            }
            frame.createBufferStrategy(2);
            final BufferStrategy bufferStrategy = frame.getBufferStrategy();

            Thread renderingThread = new Thread(){
                public void run(){
                    while (true){
                        long startRenderingTime = System.currentTimeMillis();
                        Graphics g = bufferStrategy.getDrawGraphics();
                        g.setColor(Color.white);
                        g.fillRect(0,0,1680,1050);
                        //button.paint(g);
                        //button.paintAll(g);
                        // I don't know how to draw button!
                        g.dispose();
                        if (!bufferStrategy.contentsLost()){
                            bufferStrategy.show();
                        }
                        long endRenderingTime = System.currentTimeMillis();
                        long actionTime = endRenderingTime-startRenderingTime;
                        try {
                            if (actionTime>delay){
                                sleep(5);
                            } else {
                                sleep(delay-actionTime);
                            }
                        } catch (InterruptedException e){
                            return;
                        }
                    }
                }
            };
            renderingThread.start();
        }
    });
}
4

1 回答 1

1

你在 Swings EDT 上运行一个无限循环,有效地阻止了 Swing 做任何事情。

当您想要显示 Swing 元素时,我看不到您甚至需要 BufferStrategy 什么。要将自定义渲染与 Swing 组件结合起来,您通常只需创建一个渲染您的内容的组件并将其添加到正常布局中。

您的组件只是覆盖 paintComponent() 并绘制它需要的任何内容。您可以通过调用 repaint() 轻松触发组件的更新。这通常表现得足够好。请注意,Swing 组件默认情况下是双缓冲的,因此无需在此处使用 BufferStrategy。

如果您想坚持主动渲染,您可以有选择地调用 Swings 渲染链,例如,您可以获取 Frame 的 ContentPane 并在渲染循环中调用 paint()。但是以这种方式破解它可能会导致不必要的副作用。

于 2014-06-27T16:44:41.777 回答