I want to program a simple Snake.
Therefore I have programmed a custom JPanel, which can hold a Scene.
A Scene just draws something, and you can thread it with the public void run() method, so it implements Runnable.
Now, when I initialise the Scene, I create a Thread of the instance.
if (this.getThread() == null) {
Thread sceneThread = new Thread(this);
this.setThread(sceneThread);
this.getThread().run();
} else {
System.err.println("Scene is already running");
}
And the scene finally begins to be executed in a separate thread:
// Run thread
public void run () {
try {
while (true) {
this.update();
this.getGamePanel().sceneShouldRepaint();
Thread.sleep(this.getFps());
}
}
catch (Exception e) {
System.err.println(e);
}
}
Somehow this is blocking the windows thread.
It does not appear anymore.
Can anyone tell me why?
You are not starting the thread but directly invoke its
runmethod, thus you are blocking the event thread itself in an endless loop – try starting it by callingstart()instead.Plus be sure to read about multithreading in Swing applications as pointed out by Qwerky.