我现在编写了一个简单的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();
}
});
}