There is a little hint to those who programs animation in java. The common technique is to create separate thread in which the animation loop is running. But it is possible to avoid thread creation and run animation loop directly in main thread (that is awt event dispatch thread). The main idea is to use method SwingUtilities.invokeLater to avoid blocking of main thread.
Here is code sample:
public class MyApplet extends JApplet implements Runnable {
private boolean moving;
public void start() {
moving = true;
run();
}
public void stop() {
moving = false;
}
public void run() {
if (moving) {
nextCycle();
//temporarily returning control to AWT thread
//allowing all ui events to be delivered
SwingUtilities.invokeLater(this);
}
}
public void nextCycle() {
//All animation is performed here
}
}
Really simple! No synchronization needed, no notify/wait.
You can see working examples of this approach on my applets page:
http://coolapps.hut1.ru