Painting the Background of a Container with an Image Pattern

This example shows how to use an image pattern to fill the background of a container instead of a solid color. The method used in this example makes use of a TexturePaint object to paint the image pattern.

See also Converting an Image to a Buffered Image.

public class ImageBgPanel extends JPanel { TexturePaint texture; ImageBgPanel() { texture = new TexturePaint(bufferedImage, new Rectangle(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight())); } // The class should override this method. public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D)g; g2d.setPaint(texture); g2d.fillRect(0, 0, getSize().width, getSize().height); } }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.