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

📄 tiledlayer.java

📁 现在大部分手机都不支持WTK2.0
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		//#ifdef polish.TiledLayer.GridType:defined			//#= this.animatedTiles[ animatedIndex ] = (${ polish.TiledLayer.GridType }) staticTileIndex;		//#else			this.animatedTiles[ animatedIndex ] = (byte) staticTileIndex;		//#endif	}	/**	 * Gets the tile referenced by an animated tile.  	 * <p>	 * 	 * Returns the tile index currently associated with the	 * animated tile.	 * 	 * @param animatedTileIndex the index of the animated tile	 * @return the index of the tile reference by the animated tile	 * @throws IndexOutOfBoundsException if the animated tile index is invalid	 * @see #setAnimatedTile(int, int)	 */	public int getAnimatedTile(int animatedTileIndex)	{		int animatedIndex = (-1 * animatedTileIndex) - 1;		return this.animatedTiles[animatedIndex ];	}	/**	 * Sets the contents of a cell.  	 * 	 * <P>	 * The contents may be set to a static tile index, an animated	 * tile index, or it may be left empty (index 0)	 * 	 * @param col the column of cell to set	 * @param row the row of cell to set	 * @param tileIndex the index of tile to place in cell	 * @throws IndexOutOfBoundsException if there is no tile with index tileIndex	 *									 or if row or col is outside the bounds of the TiledLayer grid	 * @see #getCell(int, int)	 * @see #fillCells(int, int, int, int, int)	 */	public void setCell(int col, int row, int tileIndex)	{		//#ifndef polish.skipArgumentCheck			if (tileIndex > this.numberOfTiles ) {				//#ifdef polish.debugVerbose					throw new IllegalArgumentException("invalid static tile index: " + tileIndex + " (there are only [" + this.numberOfTiles + "] tiles available.");				//#else					//# throw new IllegalArgumentException();				//#endif			}		//#endif		//#ifdef polish.TiledLayer.GridType:defined			//#= this.grid[ col ][ row ] = (${ polish.TiledLayer.GridType }) tileIndex;		//#else			this.grid[ col ][ row ] = (byte) tileIndex;		//#endif	}	/**	 * Gets the contents of a cell.  	 * 	 * <p>	 * 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.	 * 	 * @param col the column of cell to check	 * @param row the row of cell to check	 * @return the index of tile in cell	 * @throws IndexOutOfBoundsException if row or col is outside the bounds of the TiledLayer grid	 * @see #setCell(int, int, int)	 * @see #fillCells(int, int, int, int, int)	 */	public int getCell(int col, int row)	{		return this.grid[ col ][ row ];	}	/**	 * Fills a region cells with the specific tile.  The cells may be filled	 * with a static tile index, an animated tile index, or they may be left	 * empty (index <code>0</code>).	 * 	 * @param col the column of top-left cell in the region	 * @param row the row of top-left cell in the region	 * @param numCols the number of columns in the region	 * @param numRows the number of rows in the region	 * @param tileIndex the Index of the tile to place in all cells in the  specified region	 * @throws IndexOutOfBoundsException if the rectangular region defined by the parameters extends beyond the bounds of the TiledLayer grid	 *									 or if there is no tile with index tileIndex	 * @throws IllegalArgumentException if numCols is less than zero	 *									or if numRows is less than zero	 * @see #setCell(int, int, int)	 * @see #getCell(int, int)	 */	public void fillCells(int col, int row, int numCols, int numRows, int tileIndex)	{		//#ifndef polish.skipArgumentCheck			if (tileIndex > this.numberOfTiles) {				//#ifdef polish.debugVerbose					throw new IllegalArgumentException("invalid static tile index: " + tileIndex + " (there are only [" + this.numberOfTiles + "] tiles available.");				//#else					//# throw new IllegalArgumentException();				//#endif			}		//#endif		int endCols = col + numCols;		int endRows = row + numRows;		for (int i = col; i < endCols; i++ ) {			for (int j = row; j < endRows; j++) {				//#ifdef polish.TiledLayer.GridType:defined					//#= this.grid[ i ][ j] = (${ polish.TiledLayer.GridType }) tileIndex;				//#else					this.grid[ i ][ j ] = (byte) tileIndex;				//#endif			}		}	}	/**	 * Gets the width of a single cell, in pixels.	 * 	 * @return the width in pixels of a single cell in the  TiledLayer grid	 */	public final int getCellWidth()	{		return this.cellWidth;	}	/**	 * Gets the height of a single cell, in pixels.	 * 	 * @return the height in pixels of a single cell in the  TiledLayer grid	 */	public final int getCellHeight()	{		return this.cellHeight;	}	/**	 * Gets the number of columns in the TiledLayer grid.	 * The overall width of the TiledLayer, in pixels,	 * may be obtained by calling <A HREF="../../../../de/enough/polish/ui/game/Layer.html#getWidth()"><CODE>Layer.getWidth()</CODE></A>.	 * 	 * @return the width in columns of the  TiledLayer grid	 */	public final int getColumns()	{		return this.gridColumns;	}	/**	 * Gets the number of rows in the TiledLayer grid.  The overall	 * height of the TiledLayer, in pixels, may be obtained by	 * calling <A HREF="../../../../de/enough/polish/ui/game/Layer.html#getHeight()"><CODE>Layer.getHeight()</CODE></A>.	 * 	 * @return the height in rows of the  TiledLayer grid	 */	public final int getRows()	{		return this.gridRows;	}	/**	 * Draws the TiledLayer.	 * 	 * The entire TiledLayer is rendered subject to the clip region of	 * the Graphics object.	 * The TiledLayer's upper left corner is rendered at the	 * TiledLayer's current	 * position relative to the origin of the Graphics object.   The current	 * position of the TiledLayer's upper-left corner can be retrieved by	 * calling <A HREF="../../../../de/enough/polish/ui/game/Layer.html#getX()"><CODE>Layer.getX()</CODE></A> and <A HREF="../../../../javax/microedition/lcdui/game/Layer.html#getY()"><CODE>Layer.getY()</CODE></A>.	 * The appropriate use of a clip region and/or translation allows	 * an arbitrary region	 * of the TiledLayer to be rendered.	 * <p>	 * If the TiledLayer's Image is mutable, the TiledLayer is rendered	 * using the current contents of the Image, when the image has not been	 * split into single tiles	 *  (this is done when the "polish.TiledLayer.splitImage" or the "polish.TiledLayer.useBackBuffer" 	 * variable is set to true).	 * </p>	 * <p>	 * The TiledLayer implementation uses byte for the internal grid. This can be changed	 * using the "polish.TiledLayer.GridType" variable, which can either be 	 * "byte", "short" or "int". 	 * When the byte-grid is used, less memory is used but only 128 different tiles can be used.  	 * </p>	 * 	 * @param g the graphics object to draw the TiledLayer	 * @throws NullPointerException if g is null	 * @see Layer#paint(Graphics) in class Layer	 */	public final void paint( Graphics g)	{		if (!this.isVisible) {			return;		}		int clipX = g.getClipX();		int clipY = g.getClipY();		int clipWidth = g.getClipWidth();		int clipHeight = g.getClipHeight();		// set first and last column:		int firstColumn = 0;		int lastColumn;		if (this.xPosition < clipX) {			firstColumn = (clipX - this.xPosition) / this.cellWidth;			lastColumn = ((clipX - this.xPosition + clipWidth) / this.cellWidth) + 1;			//System.out.println("setting columnStart=" + firstColumn + " clipWidth=" + clipWidth + " cellWidth=" + this.cellWidth );		} else {			lastColumn = (clipWidth / this.cellWidth) + 1; 		}		if (lastColumn > this.gridColumns) {			lastColumn = this.gridColumns;		}		// set first and last row:		int firstRow = 0;		int lastRow;		if (this.yPosition < clipY) {			firstRow = (clipY - this.yPosition) / this.cellHeight;			lastRow = ((clipY - this.yPosition + clipHeight) / this.cellHeight) + 1;		} else {			lastRow = (clipHeight / this.cellHeight) + 1;		}		if (lastRow > this.gridRows) {			lastRow = this.gridRows;		}				// set initial x and y positions:		int xStart = this.xPosition + (firstColumn * this.cellWidth);		int yStart = this.yPosition + (firstRow * this.cellHeight);				 		int y = yStart;		int x = xStart;				//#ifdef polish.TiledLayer.GridType:defined			//#= ${ polish.TiledLayer.GridType }[][] gridTable = this.grid;		//#else			byte[][] gridTable = this.grid;		//#endif		//#ifdef tmp.useBackBuffer			x = 0;			y = 0;			// create back buffer when it is null:			boolean clearBuffer = false;			if (this.bufferGrid == null) {				clearBuffer = true;				//#ifdef polish.TiledLayer.GridType:defined					//#= this.bufferGrid = new ${ polish.TiledLayer.GridType }[ this.gridColumns ][ this.gridRows ];				//#else					this.bufferGrid = new byte[ this.gridColumns ][ this.gridRows ];				//#endif				this.bufferImage = Image.createImage( 					this.cellWidth * ((clipWidth / this.cellWidth) + 2),					this.cellHeight * ((clipHeight / this.cellHeight) + 2) );				this.bufferGraphics = this.bufferImage.getGraphics();				this.bufferFirstColumn = firstColumn;				this.bufferFirstRow = firstRow;			} else if ( this.bufferFirstColumn != firstColumn 					|| this.bufferFirstRow != firstRow) 			{				//#debug				System.out.println("clearing backbuffer");				clearBuffer = true;				this.bufferFirstColumn = firstColumn;				this.bufferFirstRow = firstRow;			}			Graphics originalGraphics = g;			g = this.bufferGraphics;			g.setColor( this.bufferTransparentColor );			if (clearBuffer) {				g.fillRect( 0, 0, this.bufferImage.getWidth(), this.bufferImage.getHeight() );			}		//#elif tmp.splitTiles && polish.api.nokia-ui && ! tmp.useBackBuffer			DirectGraphics dg = DirectUtils.getDirectGraphics(g);		//#endif				// now paint the single tiles either directly to the screen		// or to the backbuffer:		for (int column = firstColumn; column < lastColumn; column++) {			//#ifdef polish.TiledLayer.GridType:defined				//#= ${ polish.TiledLayer.GridType }[] gridColumn = gridTable[column];			//#else				byte[] gridColumn = gridTable[column];			//#endif			for (int row = firstRow; row < lastRow; row++) {				int cellIndex = gridColumn[row];				if (cellIndex != 0) {					// okay this cell needs to be rendered:					int tileIndex;					if (cellIndex < 0) {						tileIndex = this.animatedTiles[ (-1 * cellIndex ) -1  ] - 1;					} else {						tileIndex = cellIndex -1;					}					//#ifdef tmp.useBackBuffer						if (!clearBuffer && tileIndex == this.bufferGrid[ column ][ row ]) {							// the tile has been painted already to the backbuffer,							// so continue this loop:							if (!(row >= this.bufferLastRow || column >= this.bufferLastColumn)) {								y += this.cellHeight;								continue;							}						} 					//#endif					// now draw the tile:					//#ifndef tmp.splitTiles						// paint the tile the traditional way:						// slower but using less memory,						// this is also the only way for plain MIDP1-devices						// to keep the transparency of tiles: 						g.setClip( x, y, this.cellWidth, this.cellHeight );						//#ifndef tmp.useBackBuffer							// 4 variables would need to be compared, so set the 							// intersection with the original clip in all cases: 							g.clipRect( clipX, clipY, clipWidth, clipHeight );						//#endif						int tileColumn = tileIndex % this.numberOfColumns;						int tileRow = tileIndex / this.numberOfColumns; 						int tileX = x - this.tileXPositions[ tileColumn ];						int tileY = y - this.tileYPositions[ tileRow ];						g.drawImage( this.image, tileX, tileY, Graphics.TOP | Graphics.LEFT );					//#else						// paint a single tile: faster, but needs more memory.						// Also any transparency is lost, when no 						// Nokia-UI API is available:						Image tile = this.tiles[ tileIndex ];						//#ifdef tmp.useBackBuffer							g.drawImage( tile, x, y, Graphics.TOP | Graphics.LEFT );						//#elif polish.api.nokia-ui							dg.drawImage( tile, x, y, Graphics.TOP | Graphics.LEFT, 0 );						//#else							g.drawImage( tile, x, y, Graphics.TOP | Graphics.LEFT );						//#endif					//#endif					//#ifdef tmp.useBackBuffer						this.bufferGrid[ column ][ row ] = (byte) tileIndex;					//#endif				//#ifndef tmp.useBackBuffer					//# }				//#else					} else if (this.bufferGrid[ column ][ row ] != 0) {						// fill this cell with the background-color,						// the color has been set before this loop:						g.fillRect( x, y, this.cellWidth, this.cellHeight );						this.bufferGrid[ column ][ row ] = 0;					}				//#endif				y += this.cellHeight;			} // for each row			//#ifdef tmp.useBackBuffer				y = 0;			//#else				y = yStart;			//#endif			x += this.cellWidth;		} // for each column				//#ifdef tmp.useBackBuffer			// now paint the backbuffer to the screen:			originalGraphics.drawImage( this.bufferImage, xStart, yStart, Graphics.TOP | Graphics.LEFT );			// save last row and column for the next paint() call:			this.bufferLastColumn = lastColumn;			this.bufferLastRow = lastRow; 		//#endif				// reset original clip:		//#ifndef tmp.splitTiles			g.setClip( clipX, clipY, clipWidth, clipHeight );		//#endif	}		/**	 * Retrieves the tile at the given positions.	 * When the specified position is not within this	 * tiled layer, 0 is returned.	 * 	 * @param x the x position	 * @param y the y position	 * @return the tile index which is at the given position:	 * 		0 when there is no tile, a positive integer for a normal tile,	 *      a negative integer for an animated tile.	 */	protected int getTileAt( int x, int y) {		int column = (x - this.xPosition) / this.cellWidth;		int row = (y - this.yPosition) / this.cellHeight;		if (column < 0 || column >= this.gridColumns				|| row < 0 || row >= this.gridRows ) {			return 0;		} else {			return this.grid[column][row];		}	}}

⌨️ 快捷键说明

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