Alternative to drawImage to draw a tile as a texture?

Hello, I am new to Java 2d and have been building a program to learn it. I have been drawing to a JPanel overriding the paint method. First a solid color, then an image, then a section of the image (I hear its called a tile), now I was trying to draw that section as a “texture” on a arbitrary section of the panel, but there is no drawImage function for that, becuase if the rectangle area is bigger than the tile then the tile gets scaled to fit instead of replicated.

I googled and found “TexturePaint” but also a lot of people saying its way too slow. Is there a better function for this or would I have to draw it manually calling drawImage several times? (yawn)

Hmm, what If I wanted to draw a tile on a rectangle section that would allow lets say 10 tiles horizontally, but I only want to draw it twice, is manual drawing the only way for that case? ( i was thinking on a if before the drawing that would reduce the rectangle area in this case, so that the texture filling could be used for this too)

Something like drawImage( image, dest x1, dest y1, dest x2, dest y2, src image x1, src image y1, src image x2, src image y2, repeat-x, repeat-y ), where repeat-x/y can be set to a finite or infinite value (although if it is just infinite it would work for me).

EDIT: Yay I managed to code it “manually”, this works as I want, although I believe the app was just a bit slower than usual, so if there’s a better way to do this please tell me:

Vector3D and Vector2D hold x,y and z, 3D uses doubles (for 3d apps) and 2D uses long (for 2d apps).
“renderPos” already has the “point” where I want to render.and “size” the rectagular area’s size I want to render to, both define the rectangular area that has to be textured.

“material” = sprite = wrapper for the texture, the texture is a tile image.
getRectStart and getRectEnd have the x1,y1 x2,y2 coordinates in the texture of the tile I want to draw.

I implemented features that remind me of CSS: repeat-x/y and x/y-offset, repeat-x is more like “draw this many times in x”, so a value of zero is taken as do not draw, and “infinite” times is Integer.MAX_VALUE.

offset is the texture offset relative to the top left corner of the rectangle area, it doesn’t have “standard” behavior but I’m liking the current behavior.


	public void RenderQuad( java.awt.Graphics2D g, app.math.Vector3D renderPos, app.math.Vector3D size )
	{
		int timesRepeatedOnX = 0;
		int timesRepeatedOnY = 0;
		
		if( material.getRepeatOnX() == 0 || material.getRepeatOnY() == 0 ) return;
		
		app.math.Vector2D x1 = new app.math.Vector2D();
		x1.x = (int) Math.round( renderPos.x + material.getXOffset() );
		x1.y = (int) Math.round( renderPos.y + material.getYOffset() );
		x1.z = 0;
		
		app.math.Vector2D x2 = new app.math.Vector2D();
		x2.x = x1.x + material.getTileWidth();
		x2.y = x1.y + material.getTileHeight();
		x2.z = 0;
		
		app.math.Vector2D finalx2 = new app.math.Vector2D();
		finalx2.x = (int)Math.round(renderPos.x + size.x);
		finalx2.y = (int)Math.round(renderPos.y + size.y);
		finalx2.z = 0;
		
		java.awt.Rectangle r = g.getClipBounds();
		g.setClip( (int) Math.round(renderPos.x), (int) Math.round(renderPos.y), (int)Math.round(size.x), (int)Math.round(size.y) );
		
		Texture t = getTextureToRender();
		while( true )
		{
			g.drawImage( t.get(), (int)x1.x, (int)x1.y, (int)x2.x, (int)x2.y, (int)getRectStart().x, (int)getRectStart().y, (int)getRectEnd().x, (int)getRectEnd().y, null );
			
			x1.x = x2.x;
			//x1.y = x2.y;
			//x1.z = 0;
			
			x2.x += material.getTileWidth();
			
			++timesRepeatedOnX;
			if( x1.x >= finalx2.x || timesRepeatedOnX >= material.getRepeatOnX() )
			{
				x1.x = (int) Math.round( renderPos.x + material.getXOffset() );
				x1.y = x2.y;
				x2.x = x1.x + material.getTileWidth();
				x2.y += material.getTileHeight();
				//x2.z = 0;
				
				timesRepeatedOnX = 0;
				++timesRepeatedOnY;
			}
			if( x1.y >= finalx2.y || timesRepeatedOnY >= material.getRepeatOnY() )
			{
				break;
			}
		}
		
		g.setClip(r);
	}

FYI: in 6u10 (http://jdk6.dev.java.net) texture paint is hardware accelerated, and
is very fast.

Dmitri