Tiled Components in java - another approach.
You so on the previous page that using the fill() method wasn't the idea way to create a component with a tiled background. Then we decided to have a look at manual tiling.
The applet on your right hand side makes use of the code snippet on the previous page to paint the background image repeatedly across the components canvas. As you can see clearly there isn't a noticable improvement with this approach either.
The trouble here is that if you are painting a back ground that's 100 tiles wide and 100 tiles long you end up looping 10,000 times. The the iterations increase by an order or 2 so to paint a 200 by 200 background you need 40,000 iterations. ouch.
We can reduce the number of iteration by first tiling in the horizontal direction,
public void paintComponent(Graphics g)
{
int x, y;
int width, height;
Rectangle clip = g.getClipBounds();
width = img.getHeight(this);
height = img.getWidth(this);
if(width > 0 && height > 0)
{
System.out.println("hello");
for(x = clip.x; x < (clip.x + clip.width) ; x += width)
{
for(y = clip.y; y < (clip.y + clip.height) ; y += height)
{
g.drawImage(img,x,y,this);
System.out.println(x +"," + y);
}
}
}
}
You will see the revised version of the applet on the next page

