Create BufferedImage from Graphics2D - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Create

Description

Create BufferedImage from Graphics2D

Demo Code


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

public class Main {

  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    int width = 100;
    int height = 100;

    // Create an image that does not support transparency
    BufferedImage bimage = g2d.getDeviceConfiguration().createCompatibleImage(
        width, height, Transparency.OPAQUE);

    // Create an image that supports transparent pixels
    bimage = g2d.getDeviceConfiguration().createCompatibleImage(width, height,
        Transparency.BITMASK);/* ww w . j a v a 2  s  .  c o  m*/

    // Create an image that supports arbitrary levels of transparency
    bimage = g2d.getDeviceConfiguration().createCompatibleImage(width, height,
        Transparency.TRANSLUCENT);
  }
}

Related Tutorials