|
MID Profile | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--javax.microedition.lcdui.game.Layer | +--javax.microedition.lcdui.game.TiledLayer
A TiledLayer is a visual element composed of a grid of cells that can be filled with a set of tile images. This class allows large virtual layers to be created without the need for an extremely large Image. This technique is commonly used in 2D gaming platforms to create very large scrolling backgrounds,
A static tile set is created when the TiledLayer is instantiated;
it can also be updated
at any time using the setStaticTileSet(javax.microedition.lcdui.Image, int, int)
method.
In addition to the static tile set, the developer can also define several animated tiles. An animated tile is a virtual tile that is dynamically associated with a static tile; the appearance of an animated tile will be that of the static tile that it is currently associated with.
Animated tiles allow the developer to change the appearance of a group of cells very easily. With the group of cells all filled with the animated tile, the appearance of the entire group can be changed by simply changing the static tile associated with the animated tile. This technique is very useful for animating large repeating areas without having to explicitly change the contents of numerous cells.
Animated tiles are created using the createAnimatedTile(int)
method, which returns the
index to be used for the new animated tile. The animated tile
indices are always negative
and consecutive, beginning with -1. Once created, the static tile
associated with an
animated tile can be changed using the setAnimatedTile(int, int)
method.
The contents of each cell is specified by means of a tile index; a positive tile index refers to a static tile, and a negative tile index refers to an animated tile. A tile index of 0 indicates that the cell is empty; an empty cell is fully transparent and nothing is drawn in that area by the TiledLayer. By default, all cells contain tile index 0.
The contents of cells may be changed using setCell(int, int, int)
and
fillCells(int, int, int, int, int)
. Several
cells may contain the same tile; however, a single cell cannot
contain more than one tile.
The following example illustrates how a simple background can be
created using a TiledLayer.
setAnimatedTile(-1,
7)
.
The paint method will attempt to render the entire TiledLayer subject to the clip region of the Graphics object; the upper left corner of the TiledLayer is rendered at its current (x,y) position relative to the Graphics object's origin. The rendered region may be controlled by setting the clip region of the Graphics object accordingly.
Constructor Summary | |
TiledLayer(int columns,
int rows,
Image image,
int tileWidth,
int tileHeight)
Creates a new TiledLayer. |
Method Summary | |
int |
createAnimatedTile(int staticTileIndex)
Creates a new animated tile and returns the index that refers to the new animated tile. |
void |
fillCells(int col,
int row,
int numCols,
int numRows,
int tileIndex)
Fills a region cells with the specific tile. |
int |
getAnimatedTile(int animatedTileIndex)
Gets the tile referenced by an animated tile. |
int |
getCell(int col,
int row)
Gets the contents of a cell. |
int |
getCellHeight()
Gets the height of a single cell, in pixels. |
int |
getCellWidth()
Gets the width of a single cell, in pixels. |
int |
getColumns()
Gets the number of columns in the TiledLayer grid. |
int |
getRows()
Gets the number of rows in the TiledLayer grid. |
void |
paint(Graphics g)
Draws the TiledLayer. |
void |
setAnimatedTile(int animatedTileIndex,
int staticTileIndex)
Associates an animated tile with the specified static tile. |
void |
setCell(int col,
int row,
int tileIndex)
Sets the contents of a cell. |
void |
setStaticTileSet(Image image,
int tileWidth,
int tileHeight)
Change the static tile set. |
Methods inherited from class javax.microedition.lcdui.game.Layer |
getHeight, getWidth, getX, getY, isVisible, move, setPosition, setVisible |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public TiledLayer(int columns, int rows, Image image, int tileWidth, int tileHeight)
The TiledLayer's grid will be rows
cells high and
columns
cells wide. All cells in the grid are initially
empty (i.e. they contain tile index 0). The contents of the grid may
be modified through the use of setCell(int, int, int)
and fillCells(int, int, int, int, int)
.
The static tile set for the TiledLayer is created from the specified Image with each tile having the dimensions of tileWidth x tileHeight. The width of the source image must be an integer multiple of the tile width, and the height of the source image must be an integer multiple of the tile height; otherwise, an IllegalArgumentException is thrown;
The entire static tile set can be changed using
setStaticTileSet(Image, int, int)
.
These methods should be used sparingly since they are both
memory and time consuming.
Where possible, animated tiles should be used instead to
animate tile appearance.
columns
- the width of the TiledLayer
,
expressed as a number of cellsrows
- the height of the TiledLayer
,
expressed as a number of cellsimage
- the Image
to use for creating
the static tile settileWidth
- the width in pixels of a single tiletileHeight
- the height in pixels of a single tileNullPointerException
- if image
is null
IllegalArgumentException
- if the number of rows
or columns
is less than 1
IllegalArgumentException
- if tileHeight
or tileWidth
is less than 1
IllegalArgumentException
- if the image
width is not an integer multiple of the tileWidth
IllegalArgumentException
- if the image
height is not an integer multiple of the tileHeight
Method Detail |
public int createAnimatedTile(int staticTileIndex)
The indices for animated tiles are always negative. The first animated tile shall have the index -1, the second, -2, etc.
staticTileIndex
- the index of the associated tile
(must be 0
or a valid static tile index)IndexOutOfBoundsException
- if the
staticTileIndex
is invalidpublic void setAnimatedTile(int animatedTileIndex, int staticTileIndex)
animatedTileIndex
- the index of the animated tilestaticTileIndex
- the index of the associated tile
(must be 0
or a valid static tile index)IndexOutOfBoundsException
- if the
staticTileIndex
is invalidIndexOutOfBoundsException
- if the animated tile index
is invalidgetAnimatedTile(int)
public int getAnimatedTile(int animatedTileIndex)
Returns the tile index currently associated with the animated tile.
animatedTileIndex
- the index of the animated tileIndexOutOfBoundsException
- if the animated tile index
is invalidsetAnimatedTile(int, int)
public void setCell(int col, int row, int tileIndex)
The contents may be set to a static tile index, an animated tile index, or it may be left empty (index 0)
col
- the column of cell to setrow
- the row of cell to settileIndex
- the index of tile to place in cellIndexOutOfBoundsException
- if there is no tile with index
tileIndex
IndexOutOfBoundsException
- if row
or
col
is outside the bounds of the
TiledLayer
gridgetCell(int, int)
,
fillCells(int, int, int, int, int)
public int getCell(int col, int row)
Gets the index of the static or animated tile currently displayed in a cell. The returned index will be 0 if the cell is empty.
col
- the column of cell to checkrow
- the row of cell to checkIndexOutOfBoundsException
- if row
or
col
is outside the bounds of the
TiledLayer
gridsetCell(int, int, int)
,
fillCells(int, int, int, int, int)
public void fillCells(int col, int row, int numCols, int numRows, int tileIndex)
0
).col
- the column of top-left cell in the regionrow
- the row of top-left cell in the regionnumCols
- the number of columns in the regionnumRows
- the number of rows in the regiontileIndex
- the Index of the tile to place in all cells in the
specified regionIndexOutOfBoundsException
- if the rectangular region
defined by the parameters extends beyond the bounds of the
TiledLayer
gridIllegalArgumentException
- if numCols
is less
than zeroIllegalArgumentException
- if numRows
is less
than zeroIndexOutOfBoundsException
- if there is no tile with
index tileIndex
setCell(int, int, int)
,
getCell(int, int)
public final int getCellWidth()
TiledLayer
gridpublic final int getCellHeight()
TiledLayer
gridpublic final int getColumns()
Layer.getWidth()
.TiledLayer
gridpublic final int getRows()
Layer.getHeight()
.TiledLayer
gridpublic void setStaticTileSet(Image image, int tileWidth, int tileHeight)
Replaces the current static tile set with a new static tile set.
See the constructor TiledLayer(int, int, Image, int, int)
for information on how the tiles are created from the
image.
If the new static tile set has as many or more tiles than the previous static tile set, the the animated tiles and cell contents will be preserve. If not, the contents of the grid will be cleared (all cells will contain index 0) and all animated tiles will be deleted.
image
- the Image
to use for creating the
static tile settileWidth
- the width in pixels of a single tiletileHeight
- the height in pixels of a single tileNullPointerException
- if image
is null
IllegalArgumentException
- if tileHeight
or tileWidth
is less than 1
IllegalArgumentException
- if the image
width is not an integer multiple of the tileWidth
IllegalArgumentException
- if the image
height is not an integer multiple of the tileHeight
public final void paint(Graphics g)
Layer.getX()
and Layer.getY()
.
The appropriate use of a clip region and/or translation allows
an arbitrary region
of the TiledLayer to be rendered.
If the TiledLayer's Image is mutable, the TiledLayer is rendered using the current contents of the Image.
paint
in class Layer
g
- the graphics object to draw the TiledLayer
NullPointerException
- if g
is null
|
MID Profile | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |