⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tiledlayer.java

📁 J2me唆哈的代码
💻 JAVA
字号:
/*
 * Created on 2005-12-20 by pcy
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package javax.microedition.lcdui.game;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class TiledLayer extends Layer {

    private int cellHeight; // = 0;

    private int cellWidth; // = 0;

    private int rows; // = 0;

    private int columns; // = 0;

    private int[][] cellMatrix; // = null;

    Image sourceImage; // = null;

    private int numberOfTiles; // = 0;

    int[] tileSetX;

    int[] tileSetY;

    private int[] anim_to_static; // = null;

    private int numOfAnimTiles; // = 0

    public TiledLayer(int columns, int rows, Image image, int tileWidth,
            int tileHeight) {
        super(columns < 1 || tileWidth < 1 ? -1 : columns * tileWidth, rows < 1
                || tileHeight < 1 ? -1 : rows * tileHeight);

        // if img is null img.getWidth() will throw NullPointerException
        if (((image.getWidth() % tileWidth) != 0)
                || ((image.getHeight() % tileHeight) != 0)) {
            throw new IllegalArgumentException();
        }
        this.columns = columns;
        this.rows = rows;

        cellMatrix = new int[rows][columns];

        int noOfFrames = (image.getWidth() / tileWidth)
                * (image.getHeight() / tileHeight);
        // the zero th index is left empty for transparent tile
        // so it is passed in createStaticSet as noOfFrames + 1
        // Also maintain static indices is true
        // all elements of cellMatrix[][]
        // are set to zero by new, so maintainIndices = true
        createStaticSet(image, noOfFrames + 1, tileWidth, tileHeight, true);
    }

    public int createAnimatedTile(int staticTileIndex) {
        // checks static tile
        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) {
            // grow anim_to_static table if needed
            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=null;
            anim_to_static = new_anim_tbl;
        }
        anim_to_static[numOfAnimTiles] = staticTileIndex;
        ++numOfAnimTiles;
        return (-(numOfAnimTiles - 1));
    }
    
    public void setAnimatedTile(int animatedTileIndex, int staticTileIndex) {
        // checks static tile
        if (staticTileIndex < 0 || staticTileIndex >= numberOfTiles) {
            throw new IndexOutOfBoundsException();
        }
        // do animated tile index check
        animatedTileIndex = -animatedTileIndex;
        if (anim_to_static == null || animatedTileIndex <= 0
                || animatedTileIndex >= numOfAnimTiles) {
            throw new IndexOutOfBoundsException();
        }
        anim_to_static[animatedTileIndex] = staticTileIndex;

    }

    public int getAnimatedTile(int animatedTileIndex) {
        animatedTileIndex = -animatedTileIndex;
        if (anim_to_static == null || animatedTileIndex <= 0
                || animatedTileIndex >= numOfAnimTiles) {
            throw new IndexOutOfBoundsException();
        }

        return anim_to_static[animatedTileIndex];
    }

    public void setCell(int col, int row, int tileIndex) {

        if (col < 0 || col >= this.columns || row < 0 || row >= this.rows) {
            throw new IndexOutOfBoundsException();
        }

        if (tileIndex > 0) {
            // do checks for static tile
            if (tileIndex >= numberOfTiles) {
                throw new IndexOutOfBoundsException();
            }
        } else if (tileIndex < 0) {
            // do animated tile index check
            if (anim_to_static == null || (-tileIndex) >= numOfAnimTiles) {
                throw new IndexOutOfBoundsException();
            }
        }
        cellMatrix[row][col] = tileIndex;
    }

    public int getCell(int col, int row) {
        if (col < 0 || col >= this.columns || row < 0 || row >= this.rows) {
            throw new IndexOutOfBoundsException();
        }
        return cellMatrix[row][col];
    }

    public void fillCells(int col, int row, int numCols, int numRows,
            int tileIndex) {

        if (col < 0 || col >= this.columns || row < 0 || row >= this.rows
                || numCols < 0 || col + numCols > this.columns || numRows < 0
                || row + numRows > this.rows) {
            throw new IndexOutOfBoundsException();
        }

        if (tileIndex > 0) {
            // do checks for static tile
            if (tileIndex >= numberOfTiles) {
                throw new IndexOutOfBoundsException();
            }
        } else if (tileIndex < 0) {
            // do animated tile index check
            if (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;
            }
        }
    }

    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) {
        // if img is null img.getWidth() will throw NullPointerException
        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);

        // the zero th index is left empty for transparent tile
        // so it is passed in createStaticSet as noOfFrames + 1

        if (noOfFrames >= (numberOfTiles - 1)) {
            // maintain static indices
            createStaticSet(image, noOfFrames + 1, tileWidth, tileHeight, true);
        } else {
            createStaticSet(image, noOfFrames + 1, tileWidth, tileHeight, false);
        }
    }

    public final void paint(Graphics g) {

        if (g == null) {
            throw new NullPointerException();
        }

        if (visible) {
            int tileIndex = 0;

            // y-coordinate
            int ty = this.y;
            /*int cx=g.getClipX();
            int cy=g.getClipY();
            int cw=g.getClipWidth();
            int ch=g.getClipHeight();*/
            for (int row = 0; row < cellMatrix.length; ++row, ty += cellHeight) {

                // reset the x-coordinate at the beginning of every row
                // x-coordinate to draw tile into
                int tx = this.x;
                int totalCols = cellMatrix[row].length;
                for (int column = 0; column < totalCols; ++column, tx += cellWidth) {

                    tileIndex = cellMatrix[row][column];
                    // check the indices
                    // if animated get the corresponding
                    // static index from anim_to_static table
                    if (tileIndex == 0) { // transparent tile
                        continue;
                    } else if (tileIndex < 0) {
                        tileIndex = getAnimatedTile(tileIndex);
                    }
                    g.drawRegion(sourceImage, tileSetX[tileIndex],
                            tileSetY[tileIndex], cellWidth, cellHeight,
                            Sprite.TRANS_NONE, tx, ty, Graphics.TOP
                                    | Graphics.LEFT);
                    /*g.setClip(tx,ty, cellWidth, cellHeight);
                    g.drawImage(sourceImage,tx-tileSetX[tileIndex],ty-tileSetY[tileIndex],Graphics.LEFT|Graphics.TOP);*/
                }
            }
            //g.setClip(cx,cy,cw,ch);
        }
    }

    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) {
            // populate cell matrix, all the indices are 0 to begin with
            for (rows = 0; rows < cellMatrix.length; ++rows) {
                int totalCols = cellMatrix[rows].length;
                for (columns = 0; columns < totalCols; ++columns) {
                    cellMatrix[rows][columns] = 0;
                }
            }
            // delete animated tiles
            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;
            }
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -