Java Reference
In-Depth Information
public void exitMIDlet() {
canvas=null;
getDisplay().setCurrent(null);
destroyApp(true);
notifyDestroyed();
}
public void startApp() {
initialize();
}
public Display getDisplay() {
return Display.getDisplay(this);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
Here, the MIDlet's entry point calls
initialize
, which in turn creates the
MyGameCanvas
and sets the display's
Displayable
to the new
MyGameCanvas
instance before starting its
game loop.
This—creating a
GameCanvas
subclass that implements
Runnable
for its game loop,
setting the
GameCanvas
to be the current
Displayable
, and starting the game loop—is at the
heart of any MIDP 2.0-based game application.
Layering Visual Elements
While the introduction of the
GameCanvas
with its support for key polling, double-buffered
graphics, and the game loop is an important advancement in game programming for
Java ME, that's only half the story. The
Layer
class hierarchy and the related
LayerManager
greatly simplify how you handle graphics in your game.
A
Layer
is an abstract class representing a visible element of a game.
Layer
s must
know how to paint themselves, as well as track their position and visibility. You can
perform the following operations on a
Layer
:
• Obtain its position on the canvas using the
getX
and
getY
methods.
• Obtain its width and height using the
getWidth
and
getHeight
methods.