Java Reference
In-Depth Information
stick until the next call to getKeyStates() . Every time you call getKeyStates() , the latched
values are all cleared.
Understanding Layers
The rest of the Game API is devoted to layers . Layers are graphic elements that can be combined
to create a complete scene. You might, for example, have a background of mountains, another
background of city buildings, and several smaller items in the foreground: people, spaceships,
cars, whatever.
The technique of combining layers resembles traditional hand-drawn animations. Back-
ground and foreground images are drawn on transparent cels, which are placed one on top of
another and photographed to create the final scene.
In the Game API, an instance of the javax.microedition.lcdui.game.Layer class represents
a layer. Layer is abstract, with two concrete subclasses. Layer itself is pretty straightforward.
It has a location, a size, and can be visible or invisible. The location and size are accessed and
modified with the following methods, which are self-explanatory:
public final int getX()
public final int getY()
public final int getWidth()
public final int getHeight()
public void setPosition(int x, int y)
Layer also offers a handy method for moving relative to the current position. Pass pixel
offsets to the following method to adjust the position of the layer:
public void move(int dx, int dy)
The layer's visibility is accessed using getVisible() and setVisible() .
The last method in Layer is paint() , which is declared abstract. Subclasses override this
method to define their appearance.
Managing Layers
Before we tell you about Layer 's concrete children, we'll explain how layers are put together to
form a complete scene. You could do it yourself, maintaining a list of layers and drawing each
of them using their paint() methods. Fortunately, the Game API includes LayerManager , a class
that handles most of the details for you. To create a LayerManager , just call its no-argument
constructor.
Most of LayerManager 's job is keeping an ordered list of layers. Layers have an index, which
indicates their position front to back. A position of 0 is on top, closest to the user, while larger
indices are farther away, towards the bottom. (The order of layers is sometimes called the z order .)
Layers may be added to the bottom of the list using this method:
public void append(Layer l)
You can add a layer at a specific location using insert() :
public void insert(Layer l, int index)
Search WWH ::




Custom Search