awt
Stroking or Filling with a Texture example
With this example we shall show you how stroke or fill a simple graphic component with a texture. You might use these to enrich your application with graphics and customize them according tou your own taste
- In short, in order to fill an image with a texture, one should follow these steps:
- Create a new
ImageObserver
instance to monitor the loading of the image. - Load an image as a
BufferedImage
to use texture for the graphic. - Use
TexturePaint
to ser the buffered image as texture for your graphic.
Let’s see the code:
package com.javacodegeeks.snippets.desktop; import java.awt.Component; import java.awt.Frame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.TexturePaint; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; public class ImageTexture { static BufferedImage bufferedImage; static boolean imageLoaded = false; public static void main(String[] args) { // The ImageObserver implementation to observe loading of the image ImageObserver myImageObserver = new ImageObserver() { public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) { if ((flags & ALLBITS) != 0) { imageLoaded = true; return false; } return true; } }; // The image URL - change to where your image file is located! String imageURL = "image.png"; /** * This call returns immediately and pixels are loaded in the background * We use an ImageObserver to be notified when the loading of the image * is complete */ Image sourceImage = Toolkit.getDefaultToolkit().getImage(imageURL); sourceImage.getWidth(myImageObserver); // We wait until the image is fully loaded while(!imageLoaded) { try { Thread.sleep(100); } catch (InterruptedException e) { } } /** * Create a BufferedImage based on the loaded image * A BufferedImage is needed in order to be used as a texture. * The BufferedImage type - here BufferedImage.TYPE_INT_RGB * should be compatible with the source image type */ bufferedImage = new BufferedImage(sourceImage.getWidth(null), sourceImage.getHeight(null), BufferedImage.TYPE_INT_RGB); bufferedImage.getGraphics().drawImage(sourceImage, 0, 0, null); // Create a frame Frame frame = new Frame(); // Add a component with a custom paint method frame.add(new CustomPaintComponent()); // Display the frame int frameWidth = 300; int frameHeight = 300; frame.setSize(frameWidth, frameHeight); frame.setVisible(true); } /** * To draw on the screen, it is first necessary to subclass a Component and * override its paint() method. The paint() method is automatically called * by the windowing system whenever component's area needs to be repainted. */ static class CustomPaintComponent extends Component { public void paint(Graphics g) { // Retrieve the graphics context; this object is used to paint // shapes Graphics2D g2d = (Graphics2D) g; // Draw an oval that fills the window int x = 0; int y = 0; int width = getSize().width - 1; int height = getSize().height - 1; /** * The buffered image used to create the TexturePaint object is * scaled down/up to width w and height h. Conceptually, the scaled * down/up buffered image is first painted at (x, y) in user space, * and then replicated around it. */ TexturePaint texture = new TexturePaint(bufferedImage, new Rectangle(x, y, width, height)); g2d.setPaint(texture); /** * The coordinate system of a graphics context is such that the * origin is at the northwest corner and x-axis increases toward the * right while the y-axis increases toward the bottom. */ g2d.drawOval(x, y, width, height); // To fill the Oval with the texture use // g2d.fillOval(x, y, width, height); } } }
This was an example on how to perform stroking or filling with a texture.