所以我最近一直在做一个小游戏,我有一个奇怪的问题。当我不使用游戏时,游戏运行得非常完美 game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);但是关闭它的stop()方法我得到一个错误,我不明白为什么。
错误:
Exception in thread "thread" Program closed, processes halted
java.lang.IllegalStateException: Buffers have not been created
at sun.awt.windows.WComponentPeer.getBackBuffer(WComponentPeer.java:1018)
at java.awt.Component$FlipBufferStrategy.getBackBuffer(Component.java:4065)
at
java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Component.java:4
050)
at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:4165)
at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:4147)
at java.awt.Component$FlipBufferStrategy.getDrawGraphics(Component.java:4139)
at com.bacskai.peashooter.Game.render(Game.java:105)
at com.bacskai.peashooter.Game.run(Game.java:75)
at java.lang.Thread.run(Thread.java:748)
请提供不太复杂的解决方案,因为我还是初学者,游戏仍在进行中
完整代码:
package com.bacskai.peashooter;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable, KeyListener {
private static final long serialVersionUID = -4227990863874935837L;
JFrame frame;
static Dimension d;
Thread thread;
public static int width = 300;
public static int height = width / 16 * 9;
int scale = 3;
boolean running;
int[][] track = new int[5][900];
int playerY = 3;
int health = 3;
int score = 0;
public Game() {
d = new Dimension(width * scale, height * scale);
setPreferredSize(d);
frame = new JFrame();
}
public static void main(String[] args) {
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("Peasooter");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.frame.addKeyListener(game);
game.start();
}
private void start() {
System.out.println("Program started");
thread = new Thread(this, "thread");
running = true;
thread.start();
System.out.println("Window width: " + getWidth() + ", height: " +
getHeight());
}
public void run() {
while (running) {
update();
render();
}
}
private void stop() {
try {
frame.dispose();
thread.join();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Program closed, processes halted");
}
public void update() {
}
private void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {createBufferStrategy(3); return;}
Graphics g = bs.getDrawGraphics();
// Map
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.cyan);
g.fillRect(100, 0, 10, 490);
g.fillRect(0, 98, 900, 10);
g.fillRect(0, 196, 900, 10);
g.fillRect(0, 294, 900, 10);
g.fillRect(0, 392, 900, 10);
g.setColor(Color.red);
Font font = new Font("Default", Font.PLAIN, 50);
g.setFont(font);
g.drawString("Score: " + score, 700, 50);
// Player
g.setColor(Color.green);
// Enemies
g.setColor(Color.red);
// Projectiles
g.setColor(Color.green);
bs.show();
g.dispose();
} // End of render
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
stop();
}
}
public void keyReleased(KeyEvent e) {}
}
答案 0 :(得分:1)
这是使用thread.stop();
从旧bug report我们发现这不是问题:
另请注意,此问题难以重现,无其他后果 比控制台中的堆栈跟踪转储(没有挂起,没有报告可视化工件)。 因此,我正在降低此CR的优先级。
因此,如果您想保持简单,请更改代码块以捕获并忽略错误:
private void stop() {
try {
frame.dispose();
thread.join();
} catch (IllegalStateException e) {
//Do nothing
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Program closed, processes halted");
}
否则,如果您更喜欢使用非代码解决方案,那么您可以在启动应用程序时将此-Dsun.java2d.d3d=false
作为参数添加到您的命令中:
java -jar path_to / jar_file / myjar.jar -Dsun.java2d.d3d = false