Java Reference
In-Depth Information
The image could have been laid out several different ways to achieve the same result. Two
of the other possibilities are shown in Figure 14-3.
Figure 14-3. Other tile image layouts
Note that tile indices are numbered starting at one, while row and column numbers start
at zero.
The tiled layer itself is a grid of cells , where each cell is occupied by one tile. You specify the
number of rows and columns in the tiled layer at construction. The exact size of a tiled layer is
as follows:
width = [number of columns] x [tile width]
height = [number of rows] x [tile height]
Creating and Initializing a TiledLayer
To create a TiledLayer , supply the number of columns and rows, the source image, and the tile
dimensions to the constructor:
public TiledLayer(int columns, int rows,
Image image, int tileWidth, int tileHeight)
The image and tile dimensions describe a static tile set . You can change the static tile set on
an existing TiledLayer with the following method:
public void setStaticTileSet(Image image, int tileWidth, int tileHeight)
The number of columns and rows in a TiledLayer can be retrieved with getColumns() and
getRows() . To retrieve the tile dimensions, use getCellWidth() and getCellHeight() .
(Although the method naming isn't quite consistent, this works because the pixel size of each
cell is the same as the pixel size of the tiles.)
A TiledLayer is empty when you first create it. To assign a tile to a cell, use this method:
public void setCell(int col, int row, int tileIndex)
All the cells in the TiledLayer are initially filled with tile index 0, which indicates a blank
tile. You can retrieve the tile index of a particular cell by passing its column and row number to
getCell() . If you would like to assign the same tile to a range of cells, use the fillCells() method:
public void fillCells(int col, int row, int numCols, int numRows,
int tileIndex)
Search WWH ::




Custom Search