?? tiledlayer.java
字號:
package src;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class TiledLayer extends Layer
{
int screenW,screenH,tileWidth,tileHeight;//**************************************
public TiledLayer(int columns, int rows, Image image, int tileWidth,int tileHeight)
{
super(columns >= 1 && tileWidth >= 1 ? columns * tileWidth : -1,
rows >= 1 && tileHeight >= 1 ? rows * tileHeight : -1);
updated = true;
x_src = null;
y_src = null;
x_dest = null;
y_dest = null;
screenW=screenH=0;//***********************************
this.tileWidth=tileWidth;//***********************************
this.tileHeight=tileHeight;//*************************
if (image.getWidth() % tileWidth != 0|| image.getHeight() % tileHeight != 0)
{
throw new IllegalArgumentException();
}
else
{
this.columns = columns;
this.rows = rows;
cellMatrix = new int[rows][columns];
int noOfFrames = (image.getWidth() / tileWidth)* (image.getHeight() / tileHeight);
createStaticSet(image, noOfFrames + 1, tileWidth, tileHeight, true);
return;
}
}
public void setScreen(int w,int h)//************************************
{
screenW=w;screenH=h;
}
// public int createAnimatedTile(int staticTileIndex)
// {
// if (staticTileIndex < 0 || staticTileIndex >= numberOfTiles)
// throw new IndexOutOfBoundsException();
// if (anim_to_static == null)
// {
// anim_to_static = new int[4];
// numOfAnimTiles = 1;
// }
// else if (numOfAnimTiles == anim_to_static.length)
// {
// int new_anim_tbl[] = new int[anim_to_static.length * 2];
// System.arraycopy(anim_to_static, 0, new_anim_tbl, 0,
// anim_to_static.length);
// anim_to_static = new_anim_tbl;
// }
// anim_to_static[numOfAnimTiles] = staticTileIndex;
// numOfAnimTiles++;
//
// return -(numOfAnimTiles - 1);
// }
// public void setAnimatedTile(int animatedTileIndex, int staticTileIndex)
// {
// if (staticTileIndex < 0 || staticTileIndex >= numberOfTiles)
// throw new IndexOutOfBoundsException();
// animatedTileIndex = -animatedTileIndex;
// if (anim_to_static == null || animatedTileIndex <= 0
// || animatedTileIndex >= numOfAnimTiles)
// {
// throw new IndexOutOfBoundsException();
// }
// else
// {
// anim_to_static[animatedTileIndex] = staticTileIndex;
// return;
// }
// }
public int getAnimatedTile(int animatedTileIndex)
{
animatedTileIndex = -animatedTileIndex;
if (anim_to_static == null || animatedTileIndex <= 0
|| animatedTileIndex >= numOfAnimTiles)
throw new IndexOutOfBoundsException();
else
return anim_to_static[animatedTileIndex];
}
public void setCell(int col, int row, int tileIndex)
{
if (col < 0 || col >= columns || row < 0 || row >= rows)
throw new IndexOutOfBoundsException();
if (tileIndex > 0)
{
if (tileIndex >= numberOfTiles)
throw new IndexOutOfBoundsException();
}
else if (tileIndex < 0&& (anim_to_static == null || -tileIndex >= numOfAnimTiles))
throw new IndexOutOfBoundsException();
cellMatrix[row][col] = tileIndex;
updated = true;
}
public int getCell(int col, int row)
{
if (col < 0 || col >= columns || row < 0 || row >= rows)
throw new IndexOutOfBoundsException();
else
return cellMatrix[row][col];
}
// public void fillCells(int col, int row, int numCols, int numRows,int tileIndex)
// {
// if (numCols < 0 || numRows < 0)
// throw new IllegalArgumentException();
// if (col < 0 || col >= columns || row < 0 || row >= rows
// || col + numCols > columns || row + numRows > rows)
// throw new IndexOutOfBoundsException();
// if (tileIndex > 0)
// {
// if (tileIndex >= numberOfTiles)
// throw new IndexOutOfBoundsException();
// }
// else if (tileIndex < 0&& (anim_to_static == null || -tileIndex >= numOfAnimTiles))
// throw new IndexOutOfBoundsException();
// for (int rowCount = row; rowCount < row + numRows; rowCount++)
// {
// for (int columnCount = col; columnCount < col + numCols; columnCount++)
// cellMatrix[rowCount][columnCount] = tileIndex;
// }
// updated = true;
// }
public final int getCellWidth()
{
return cellWidth;
}
public final int getCellHeight()
{
return cellHeight;
}
public final int getColumns()
{
return columns;
}
public final int getRows()
{
return rows;
}
// public void setStaticTileSet(Image image, int tileWidth, int tileHeight)
// {
// updated = true;
// if (tileWidth < 1 || tileHeight < 1
// || image.getWidth() % tileWidth != 0
// || image.getHeight() % tileHeight != 0)
// throw new IllegalArgumentException();
// setWidthImpl(columns * tileWidth);
// setHeightImpl(rows * tileHeight);
// int noOfFrames = (image.getWidth() / tileWidth)
// * (image.getHeight() / tileHeight);
// if (noOfFrames >= numberOfTiles - 1)
// createStaticSet(image, noOfFrames + 1, tileWidth, tileHeight, true);
// else
// createStaticSet(image, noOfFrames + 1, tileWidth, tileHeight, false);
// }
public final void paint(Graphics g)
{
this.paint(g, false);
}
public final void paintLookDown(Graphics g)
{
this.paint(g, true);
}
private final void paint(Graphics g, boolean lookDown)
{
if (g == null)
throw new NullPointerException();
if (visible)
{
int tileIndex = 0;
if (updated)
{
int arrayLength = 0;
for (int row = 0; row < cellMatrix.length; row++)
{
int totalCols = cellMatrix[row].length;
for (int column = 0; column < totalCols; column++)
{
tileIndex = cellMatrix[row][column];
if (tileIndex != 0)
arrayLength++;
}
}
if (x_src == null || arrayLength > x_src.length)
{
x_src = new int[arrayLength];
y_src = new int[arrayLength];
x_dest = new int[arrayLength];
y_dest = new int[arrayLength];
}
tileArrayLength = arrayLength;
updated = false;//*********************************
}//**************************************************
//**************************
int ty = lookDown?y - this.cellHeight/2:y;
int arrayIndex = 0;
for (int row = 0; row < cellMatrix.length;)
{
int tx = lookDown?x-this.cellWidth/2:x;
int totalCols = cellMatrix[row].length;
for (int column = 0; column < totalCols;)
{
tileIndex = cellMatrix[row][column];
if (tileIndex != 0)
{
if (tileIndex < 0)
tileIndex = getAnimatedTile(tileIndex);
x_src[arrayIndex] = tileSetX[tileIndex];
y_src[arrayIndex] = tileSetY[tileIndex];
x_dest[arrayIndex] = tx;
y_dest[arrayIndex] = ty;
arrayIndex++;
}
column++;
tx += lookDown?cellWidth/2:cellWidth;
}
row++;
ty += lookDown?cellHeight/2:cellHeight;
}
// drawTiledRegion(g, sourceImage, tileArrayLength, x_src, y_src, cellWidth, cellHeight, 0, x_dest, y_dest, 20);
for(int i=0; i<tileArrayLength; i++)
{
if(x_dest[i]>0-tileWidth&&x_dest[i]<screenW&&y_dest[i]>0-tileHeight&&y_dest[i]<screenH)
{
g.setClip(this.x_dest[i], this.y_dest[i], this.cellWidth, this.cellHeight);
g.drawImage(this.sourceImage, this.x_dest[i] - x_src[i], this.y_dest[i] - y_src[i], Graphics.LEFT | Graphics.TOP);
}
}
}
}
// private void drawTiledRegion(Graphics g, Image image, int tileArrayLength, int x_src[], int y_src[], int cellWidth, int cellHeight, int l, int x_dest[], int y_dest[], int i1){
//
// }
private void createStaticSet(Image image, int noOfFrames, int tileWidth,int tileHeight, boolean maintainIndices)
{
cellWidth = tileWidth;
cellHeight = tileHeight;
int imageW = image.getWidth();
int imageH = image.getHeight();
sourceImage = image;
numberOfTiles = noOfFrames;
tileSetX = new int[numberOfTiles];
tileSetY = new int[numberOfTiles];
if (!maintainIndices)
{
for (rows = 0; rows < cellMatrix.length; rows++)
{
int totalCols = cellMatrix[rows].length;
for (columns = 0; columns < totalCols; columns++)
cellMatrix[rows][columns] = 0;
}
anim_to_static = null;
}
int currentTile = 1;
for (int y = 0; y < imageH; y += tileHeight)
{
for (int x = 0; x < imageW; x += tileWidth)
{
tileSetX[currentTile] = x;
tileSetY[currentTile] = y;
currentTile++;
}
}
}
/**
* <code>cellHeight</code> 單元格的高度
*/
private int cellHeight;
/**
* <code>cellWidth</code> 單元格的寬度
*/
private int cellWidth;
/**
* <code>rows</code> 行數(shù)
*/
private int rows;
/**
* <code>columns</code> 列數(shù)
*/
private int columns;
/**
* <code>cellMatrix</code> 保存地圖貼圖數(shù)據(jù)索引值,第一維保存列數(shù)據(jù),第二維保存行數(shù)據(jù),每個數(shù)組元素保存一個貼圖的索引值
*/
private int cellMatrix[][];
/**
* <code>sourceImage</code> 貼圖源圖片
*/
protected Image sourceImage;
/**
* <code>numberOfTiles</code> 地圖貼圖索引的總數(shù)
*/
private int numberOfTiles;
/**
* <code>tileSetX</code> 記錄每個貼圖在源圖中的起始X坐標
*/
int tileSetX[];
/**
* <code>tileSetY</code> 記錄每個貼圖在源圖中的起始Y坐標
*/
int tileSetY[];
private int anim_to_static[];
private int numOfAnimTiles;
private boolean updated;
/**
* <code>tileArrayLength</code> 需繪制的單元格總數(shù)
*/
private int tileArrayLength;
/**
* <code>x_src</code> 地圖單元格需繪制的圖片在源圖中的X坐標
*/
private int x_src[];
/**
* <code>y_src</code> 地圖單元格需繪制的圖片在源圖中的X坐標
*/
private int y_src[];
/**
* <code>x_dest</code> 每個地圖單元格的X坐標
*/
private int x_dest[];
/**
* <code>y_dest</code> 每個地圖單元格的Y坐標
*/
private int y_dest[];
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -