Java Reference
In-Depth Information
For example, you could add a layer to the top of the list by inserting a layer at index 0.
You can find the number of layers in the
LayerManager
by calling
getSize()
. If you'd like to
retrieve the layer at a certain position, pass the index to the
getLayerAt()
method.
Finally, you can remove a layer by passing the
Layer
object to the
remove()
method.
LayerManager
includes the concept of a
view window
, which is the rectangular portion of
the scene that will be drawn. The assumption is that the overall scene is larger than the screen
of the device, so only a portion will be drawn at any time. By default, the view window has its
origin at
0, 0
and is as large as it can be (
Integer.MAX_VALUE
for both width and height). You can
set the view window using the following method, where the
x
and
y
coordinates are relative to
the origin of the
LayerManager
.
public void setViewWindow(int x, int y, int width, int height)
To actually draw the scene represented by the
LayerManager
's layers, call the
paint()
method:
public void paint(Graphics g, int x, int y)
The view window of the scene will be drawn using the given
Graphics
at the specified location,
which is specified in the coordinate system of the
Graphics
.
If you're still fuzzy on the relationship between a layer manager, its layers, and its view
window, see the API documentation for
LayerManager
, which contains two very helpful figures.
Using Tiled Layers
A tiled layer is made from a palette of tiles, just as you might assemble decorative tiles to create
a pretty design next to your bathtub. The tiles come from a single image that is divided into
equal-sized pieces.
A
TiledLayer
is drawn on a
Graphics
object using the
paint()
method inherited from
Layer
.
Like any other
Layer
, a tiled layer renders itself at its current location in the coordinate system
of the
Graphics
. Furthermore, like any other
Layer
, a tiled layer can be part of a
LayerManager
and can be rendered automatically when the
LayerManager
is rendered using
its
paint()
method.
For example, Figure 14-1 is 240 pixels wide and 96 pixels high.
Figure 14-1.
A source image for a tiled layer
The image can be divided into 10 square tiles, each with a width and height of 48 pixels.
Tiles are numbered as shown in Figure 14-2.
Figure 14-2.
Tile numbering
Search WWH ::
Custom Search