tiledlayer.java

来自「用J2ME实现的战棋类小游戏DEMO,寻路用A星算法,因为时间关系物品使用功能请」· Java 代码 · 共 138 行

JAVA
138
字号
package midp20;

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

public class TiledLayer extends Layer {
	public int[][] mapID;
	
	private Image img;

	private int map_width = 0;

	private int map_height = 0;

	private int column;

	private int row;

	private int numRows;

	private int numColumns;

	public static int x_window = 0;

	public static int y_window = 0;

	public TiledLayer(int column, int row, Image img, int width, int height,int [][]mapID) {
		super(img, width, height);
        this.mapID=mapID;
		this.column = column;
		this.row = row;
		this.img = img;
		numRows = img.getHeight() / height;
		numColumns = img.getWidth() / width;
		// mapID = new int[row][column];

		map_width = column * width;
		map_height = row * height;
	}

	public static  void setWindowPosition(int x, int y) {
		x_window = x;
		y_window = y;
	}

	public void setCell(int column, int row, int tileIndex) {
		mapID[row][column] = tileIndex;
	}

	public int getCell(int column, int row) {
		return mapID[row][column];
	}

	public int getColumn() {
		return column;
	}

	public int getRow() {
		return row;
	}

	public int getMapWidth() {
		return map_width;
	}

	public int getMapHeight() {
		return map_height;
	}

	public void paint(Graphics g) {

		for (int i = 0; i < mapID.length; i++) {
			for (int j = 0; j < mapID[i].length; j++) {
				int startRow = y_window / getHeight();
				int startcolumn = x_window / getWidth();    
				if (j >= startcolumn+5 && j < startcolumn + 14 && i >= startRow+4
				&& i < startRow + 14) {
				int tempx = x + j * width;
				int tempy = y + i * height;

				g.setClip(tempx-TiledLayer.x_window, tempy-TiledLayer.y_window, width, height);
              
				g.drawImage(img, tempx - (mapID[i][j] % numColumns) * width
						- x_window, tempy - (mapID[i][j] / numColumns) * height
						- y_window, 0);
			}

			}
		}
	}
	
	public void painttl2(Graphics g) {

		for (int i = 0; i < mapID.length; i++) {
			for (int j = 0; j < mapID[i].length; j++) {
				int startRow = y_window / getHeight();
				int startcolumn = x_window / getWidth();    
				if (j >= startcolumn+1 && j < startcolumn + 8 && i >= startRow+1
				&& i < startRow + 8) {
				int tempx = x + j * width;
				int tempy = y + i * height;

				g.setClip(tempx-TiledLayer.x_window, tempy-TiledLayer.y_window, width, height);
              
				g.drawImage(img, tempx - (mapID[i][j] % numColumns) * width
						- x_window, tempy - (mapID[i][j] / numColumns) * height
						- y_window, 0);
			}

			}
		}
	}
	
	public void painttl3(Graphics g) {

		for (int i = 0; i < mapID.length; i++) {
			for (int j = 0; j < mapID[i].length; j++) {
//				int startRow = y_window / getHeight();
//				int startcolumn = x_window / getWidth();    
//				if (j >= startcolumn+1 && j < startcolumn + 8 && i >= startRow+1
//				&& i < startRow + 8) {
				int tempx = x + j * width;
				int tempy = y + i * height;

				g.setClip(tempx-TiledLayer.x_window, tempy-TiledLayer.y_window, width, height);
              
				g.drawImage(img, tempx - (mapID[i][j] % numColumns) * width
						- x_window, tempy - (mapID[i][j] / numColumns) * height
						- y_window, 0);
			}

			
		}
	}

	
}

⌨️ 快捷键说明

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