Example usage for java.awt Graphics2D drawImage

List of usage examples for java.awt Graphics2D drawImage

Introduction

In this page you can find the example usage for java.awt Graphics2D drawImage.

Prototype

public abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y);

Source Link

Document

Renders a BufferedImage that is filtered with a BufferedImageOp .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");
    BufferedImage image = ImageIO.read(url);

    int w = image.getWidth();
    int h = image.getHeight();
    Ellipse2D.Double ellipse1 = new Ellipse2D.Double(10, 10, 20, 30);
    Ellipse2D.Double ellipse2 = new Ellipse2D.Double(15, 15, 20, 30);
    Area circle = new Area(ellipse1);
    circle.subtract(new Area(ellipse2));

    BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = result.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.setClip(circle);/*from w ww  .j  av  a 2 s . c  o  m*/
    g.drawImage(image, 0, 0, null);
    g.dispose();

    ImageIO.write(result, "png", new File("result.png"));
}

From source file:Main.java

private static BufferedImage dye(BufferedImage image, Color color) {
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage dyed = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = dyed.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.setComposite(AlphaComposite.SrcAtop);
    g.setColor(color);//from  w  ww.  ja  va2s .com
    g.fillRect(0, 0, w, h);
    g.dispose();
    return dyed;
}

From source file:Main.java

private static BufferedImage prepareImage(BufferedImage image, int shadowSize) {
    BufferedImage subject = new BufferedImage(image.getWidth() + shadowSize * 2,
            image.getHeight() + shadowSize * 2, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2 = subject.createGraphics();
    g2.drawImage(image, null, shadowSize, shadowSize);
    g2.dispose();//from w  ww .java2 s  .  c o  m

    return subject;
}

From source file:Main.java

public static Image merge(Image left, Image right) {
    BufferedImage merged = new BufferedImage(left.getWidth(null) + right.getWidth(null), left.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) merged.getGraphics();
    g.drawImage(left, 0, 0, null);
    g.drawImage(right, left.getWidth(null), 0, null);
    return merged;
}

From source file:Main.java

/**
 * Creates a shadow mask// ww w  .  j av a 2s. com
 * @param image
 * @param shadowColor
 * @param shadowOpacity
 * @return
 */
private static BufferedImage createShadowMask(BufferedImage image, Color shadowColor, float shadowOpacity) {
    BufferedImage mask = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = mask.createGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, shadowOpacity));
    g2d.setColor(shadowColor);
    g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
    g2d.dispose();

    return mask;
}

From source file:Main.java

public static BufferedImage getFlippedImage(BufferedImage bi) {
    BufferedImage flipped = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
    AffineTransform tran = AffineTransform.getTranslateInstance(0, bi.getHeight());
    AffineTransform flip = AffineTransform.getScaleInstance(1d, -1d);
    tran.concatenate(flip);//from  w w w  .  j  a  v  a2  s . c om

    Graphics2D g = flipped.createGraphics();
    g.setTransform(tran);
    g.drawImage(bi, 0, 0, null);
    g.dispose();

    return flipped;
}

From source file:Main.java

public static BufferedImage highlightRegions(Image img, int[][] regions, int regionId, Color fgColour) {
    BufferedImage canvas = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = canvas.createGraphics();
    g2d.drawImage(img, 0, 0, null);
    g2d.setColor(fgColour);/*from w w  w. ja  v  a  2  s.c o m*/
    for (int y = 0; y < regions.length; y++) {
        for (int x = 0; x < regions[y].length; x++) {
            if (regions[y][x] == regionId) {
                g2d.drawRect(x, y, 1, 1);
            }
        }
    }

    return canvas;
}

From source file:Main.java

public static BufferedImage asCompatibleImage(Image img) {
    BufferedImage ret = defaultScreenDeviceConfiguration().createCompatibleImage(img.getWidth(null),
            img.getHeight(null));//  ww w. j  a v  a2 s .c om
    Graphics2D gc = ret.createGraphics();
    gc.drawImage(img, 0, 0, null);
    gc.dispose();
    return ret;
}

From source file:Main.java

/**
 * Shrinks an image to fit into memory more
 * Effectively.//w  w w  . j a v  a  2s .  c o  m
 * @param src The source image.
 * @return
 */
public static BufferedImage imgUtilMinimizeNoAlpha(BufferedImage src) {
    if (src == null)
        return null;
    BufferedImage b = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D g = (Graphics2D) b.getGraphics();
    g.drawImage(src, 0, 0, null);
    g.dispose();
    return b;
}

From source file:Main.java

/**
 * Applies a {@link BufferedImageOp} on the given {@link BufferedImage}.
 *
 * @param source The source image./*  w  w  w  .j a  v a 2  s . c o  m*/
 * @param op     The operation to perform.
 * @return A new image with the operation performed.
 */
public static BufferedImage operatedImage(BufferedImage source, BufferedImageOp op) {
    BufferedImage newImage = newArgbBufferedImage(source.getWidth(), source.getHeight());
    Graphics2D g = (Graphics2D) newImage.getGraphics();
    g.drawImage(source, op, 0, 0);
    return newImage;
}