The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.awt.image  [21 examples] > Buffered Images  [7 examples]

e669. Drawing on a Buffered Image

To draw on a buffered image, create a graphics context on the buffered image.
    // Create a graphics context on the buffered image
    Graphics2D g2d = bimage.createGraphics();
    
    // Draw on the image
    g2d.setColor(Color.red);
    g2d.fill(new Ellipse2D.Float(0, 0, 200, 100));
    g2d.dispose();
If the buffered image supports transparency, (see e661 Determining If an Image Has Transparent Pixels), pixels can be made transparent:
    g2d = bimage.createGraphics();
    
    // Make all filled pixels transparent
    Color transparent = new Color(0, 0, 0, 0);
    g2d.setColor(transparent);
    g2d.setComposite(AlphaComposite.Src);
    g2d.fill(new Rectangle2D.Float(20, 20, 100, 20));
    g2d.dispose();

 Related Examples
e666. Creating a Buffered Image
e667. Creating a Buffered Image from an Image
e668. Creating a Buffered Image from an Array of Color-Indexed Pixel Values
e670. Converting a Buffered Image to an Image
e671. Getting and Setting Pixels in a Buffered Image
e672. Scaling, Shearing, Translating, and Rotating a Buffered Image

See also: Effects    Images    Volatile Images   


© 2002 Addison-Wesley.