Java :: Swing :: JComponent - Tiled Background Images.

Have you seen those beautifull user interfaces on the macintosh? Oh sure they don't have a backspace key but you can't deny the fact that those 'tv lined' backgrounds on the UIs look really good.

I wanted to create such UIs with java swing on other platforms as well. So having armed myself with 4x1 pixel image i decided to create the following applet.


	public void paintComponent(Graphics g)
	{
		if(paint == null)
		{
			try {
				/* img is the background tile */
				int height = img.getHeight(this);
				int width = img.getWidth(this);
				bim = (BufferedImage) createImage(width,height);

				Graphics2D bim_g2d = (Graphics2D) bim.getGraphics();
				bim_g2d.drawImage(img,0,0,Color.black,this);

				paint = new TexturePaint(bim,new Rectangle(0,0,width,height));
			}
			catch (Exception ex) {
				/* method called before bg image has been loaded */

			}
		}
		Graphics2D g2d = (Graphics2D) g;
		if(paint != null)
		{
			g2d.setPaint(paint);
			g2d.fill(g2d.getClip());
		}
	}


The applet on the next page uses this code. As you will see it takes way too long for the background to be painted with this approach