Coder's Guild Mailing List

Re: tiling an image using java

Posted by Kspansel@xxx.xxx on 1999-07-10

> Does anyone know how to tile an image using java? 

This is what I do:

1) Load the image your going to tile.
	Image Tile = getImage(getCodeBase(), "someImage.gif");
2) Get Image Width and Height..
	TileWidth = Tile.getWidth(this);
	TileHeight = Tile.getHeight(this);
3) Depending upon where your goign to tile the image, find the number up and 
down..here I would tile the whole applet..
	int NumberX = (int)(this.size().width / TileWidth);
	int NumberY = (int)(this.size().height / TileHeight);
4) Thne loop across and down drawing each tile..I recommend drawing to 
anothing image so you don't have to loop thru and draw each tile everytime 
you paint.  Like so:
	Image Buffer = createImage(NumberX * TileWidth, NumberY * TileHeight);
	Graphics BufferG = Buffer.getGraphics();

	for (int i = 0;i <= NumberY; i++)
	{
		for (int j = 0;j <= NumberX; j++)
		{
			BufferG.drawImage(Tile, j*TileWidth, i*TileHeight, 
TileWidth, TileHeight, this);
		}
	}
5) The just clip it to the applet (I not sure if that's even necessary), the 
draw the image.
	g.clipRect(0, 0, this.size().width, this.size().height);
	g.drawImage(Buffer, 0, 0, this);

And it's just like that..I tried to explain it as simple as possible, for 
some others if they also wanted to know.  Cause, Frank, I know you can 
understand it..no matter how I write it.  hehe.  And comments and suggestions 
welcome.  Thanks.  Later!

Kory Spansel