Creating simple animations |
Animations take a bit of work, as they require a separate thread to do the updating of the picture that is different from the thread that is actually doing the drawing and responding to other events. The following is a description of a low overhead way of doing animation that relies on events fired by a timer rather than creating a separate thread (which we will talk about later in the term). See the program SimpleTimerAnimation for a simple example of an application that creates an animation using a Timer object.
Timer myTimer = new Timer(interval,this); myTimer.start();
This creates a timer that will generate an ActionEvent every interval milliseconds (which is 1/1000 second). The timer can be turned off by sending it a stop() message.
public class MyClass extends JFrame implements ActionListener {
The interface ActionListener has a single method:
public void actionPerformed(ActionEvent evt);
that the class must implement.
Creating simple animations |