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

📄 tiledlayer.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	 */
	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 + -