Drawing on a Buffered Image - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Paint

Description

Drawing on a Buffered Image

Demo Code

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

public class Main {
  public static void main(String[] argv) throws Exception {
    BufferedImage bimage = null;/*w ww  .j a  va2s  .c  o m*/
    Graphics2D g2d = bimage.createGraphics();

    // Draw on the image
    g2d.setColor(Color.red);
    g2d.fill(new Ellipse2D.Float(0, 0, 200, 100));
    g2d.dispose();
    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 Tutorials