Java Reference
In-Depth Information
•
append
: Takes a
Layer
and places it at the end of the
LayerManager
's list
•
insert
: Takes a
Layer
and an index and inserts the
Layer
at that index in the
LayerManager
's list, sliding subsequent
Layer
s behind the inserted
Layer
•
getLayerAt
: Takes an index and returns the
Layer
at that index
•
remove
: Takes a
Layer
and removes that
Layer
from the
LayerManager
's list
•
getSize
: Returns the number of
Layer
s in the
LayerManager
's list
■
Caution
Remember that the order in which you add items to the
LayerManager
's list determines the
Z-order for drawing, and the front-most item is at index
0
—the first item you add to the list! It's not uncom-
mon to build your list of
Layer
s, only to find that the background is the only thing visible, because you built
your list in the reverse order.
You also use the
LayerManager
to present the contents of its list to the user on the
display by invoking its
paint
method. When you invoke
paint
, the
LayerManager
renders
each of its layers in order of descending index, implementing the Z-order promised by
the interface. The
paint
method takes the
Graphics
instance to use when rendering the
Layers
in the list, and the offset in the
Graphics
instance where drawing should take place.
You use the
setViewWindow
method to set the clipping region that the
LayerManager
uses.
■
Tip
The
LayerManager.paint
method is optimized; it won't render items that are completely outside
the
Graphics
clipping region. Thus, if you create a custom
Layer
subclass and implement the
paint
method, you should be aware that the
paint
method is
only
invoked if there's something to paint.
Optimizing Visual Layers Using Tiling
Most games have at least one visual component that can consist of regularly repeating
bitmaps, such as the background for a game level. For example, consider Figure 8-2,
which shows an eight-by-eight-tile grid for a game board made up of three tiles.