我想編寫一個簡單的Snake。 因此我編寫了一個自定義JPanel
,它可以容納一個Scene
。 A Scene
只是繪製了一些東西,並且可以使用public void run()
方法進行線程化,所以它實現Runnable
。JPanel中的無限循環
現在,當我初始化場景時,我創建了一個Thread
實例。
if (this.getThread() == null) {
Thread sceneThread = new Thread(this);
this.setThread(sceneThread);
this.getThread().run();
} else {
System.err.println("Scene is already running");
}
場面終於開始在一個單獨的線程中執行:
// Run thread
public void run() {
try {
while (true) {
this.update();
this.getGamePanel().sceneShouldRepaint();
Thread.sleep(this.getFps());
}
}
catch (Exception e) {
System.err.println(e);
}
}
不知怎的,這是阻止Windows線程。 它不再出現。
有誰能告訴我爲什麼?
你可以給Windows線程的代碼? –
您是否已閱讀並理解擺動線程模型如何與事件分派線程一起工作? http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html – Qwerky
@IshanKhanna我剛剛創建了一個新的GUI();','GUI'是一個'JFrame'子類 – NSAddict