我用Java创建了一个使用缓冲策略的游戏。但有时我会得到一个白色框架或白色边框。这种情况大约每五次发生一次。我搜索了很多,但我真的无法弄清楚我做错了什么。代码编译并且没有打印出任何错误。
如果失败,那么框架就像那样:
这是代码(仅相关部分):
private BufferStrategy bs;
public Testclass(){
setResizable(false);
setSize(1000,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setIgnoreRepaint(true);
setVisible(true);
createBufferStrategy(2);
bs = getBufferStrategy();
}
protected void paint() {
do {
Graphics2D g=null;
try {
g = (Graphics2D) bs.getDrawGraphics();
g.setColor(Color.CYAN);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
} finally {
g.dispose();
}
bs.show();
} while (bs.contentsLost());
}
public static void main(String[] args) {
Testclass window = new Testclass();
window.paint();
}
答案 0 :(得分:0)
我实际上找到了一个更好的方法来尝试这个:
boolean BufferStrategyRunning = true;
while(BufferStrategyRunning == true) {
BufferStrategy BS = GUIFrame.getBufferStrategy();
if(BS == null) {
GUIFrame.createBufferStrategy(3);
continue;
}
Graphics g = BS.getDrawGraphics();
g.drawImage(CurrentScreen.BackgroundImage, 0, 0, null);
g.dispose();
BS.show();
}