Java Reference
In-Depth Information
You'd kick off the animation by calling
start()
, which in turn would simply call
run()
.
Inside
run()
, you update your state and call
repaint()
to request the painting of a new frame.
Then you use
callSerially()
to request that you get called again when the painting is done.
This technique results in an animation that runs as fast as the device allows. Many appli-
cations, however, need to provide a consistent experience across different devices. In these
cases, it makes much more sense to use a separate animation thread with a consistent frame
delay. The following example demonstrates this technique. It consists of two classes,
Sweep
and
SweepCanvas
.
Sweep
is a MIDlet that displays the class that actually implements the animation,
SweepCanvas
. The running
SweepCanvas
is shown in Figure 13-9.
Figure 13-9.
SweepCanvas animation running on the WTK default emulator
First, here's the source code for
Sweep
:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class Sweep
extends MIDlet {
public void startApp() {
final SweepCanvas sweeper = new SweepCanvas();
sweeper.start();
sweeper.addCommand(new Command("Exit", Command.EXIT, 0));
sweeper.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable s) {
sweeper.stop();
notifyDestroyed();
}
});
Search WWH ::
Custom Search