Example usage for java.awt.image BufferedImage createGraphics

List of usage examples for java.awt.image BufferedImage createGraphics

Introduction

In this page you can find the example usage for java.awt.image BufferedImage createGraphics.

Prototype

public Graphics2D createGraphics() 

Source Link

Document

Creates a Graphics2D , which can be used to draw into this BufferedImage .

Usage

From source file:Utils.java

public static Shape generateShapeFromText(Font font, String string) {
    BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = img.createGraphics();

    try {// w  w  w .j  a va  2s.  c  o m
        GlyphVector vect = font.createGlyphVector(g2.getFontRenderContext(), string);
        Shape shape = vect.getOutline(0f, (float) -vect.getVisualBounds().getY());

        return shape;
    } finally {
        g2.dispose();
    }
}

From source file:Main.java

/**
 * Creates a shadow mask/*  w w w .  j a  v  a  2  s  .c  o m*/
 * @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 Image resize(Image i, int scale) {
    BufferedImage resizedImage = new BufferedImage(scale, scale, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(i, 0, 0, scale, scale, null);
    g.dispose();//from   w  w w  .j  ava 2s  .c o m
    g.setComposite(AlphaComposite.Src);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    return resizedImage;
}

From source file:Main.java

public static BufferedImage resize(int targetWidth, int targetHeight, BufferedImage src) {
    double scaleW = (double) targetWidth / (double) src.getWidth();
    double scaleH = (double) targetHeight / (double) src.getHeight();

    double scale = scaleW < scaleH ? scaleW : scaleH;

    BufferedImage result = new BufferedImage((int) (src.getWidth() * scale), (int) (src.getHeight() * scale),
            BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = result.createGraphics();
    g2d.drawImage(src, 0, 0, result.getWidth(), result.getHeight(), null);
    g2d.dispose();//  w  w w . j av a  2  s . c om

    return result;
}

From source file:Main.java

public static BufferedImage drawBoundingBoxes(BufferedImage canvas, Rectangle[] boxes, Color fgColour,
        Color bgColour) {//from w w w . j  a v a2 s.co m
    Graphics2D g2d = canvas.createGraphics();
    for (int i = 0; i < boxes.length; i++) {
        g2d.setColor(bgColour);
        g2d.drawRect(boxes[i].x - 1, boxes[i].y - 1, boxes[i].width + 2, boxes[i].height + 2);
        g2d.drawRect(boxes[i].x + 1, boxes[i].y + 1, boxes[i].width - 2, boxes[i].height - 2);
        g2d.setColor(fgColour);
        g2d.drawRect(boxes[i].x, boxes[i].y, boxes[i].width, boxes[i].height);
    }

    return canvas;
}

From source file:Main.java

public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = img;/*from w ww  .  j  av a  2  s . c om*/
    int w, h;
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        BufferedImage tmp = new BufferedImage(w, h, type);
        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}

From source file:Main.java

public static BufferedImage drawBoundingBoxes(BufferedImage canvas, List<Rectangle> boxes, Color fgColour,
        Color bgColour) {/*from   w  w  w  .j  a  v  a2  s . c  o  m*/
    Graphics2D g2d = canvas.createGraphics();
    for (Rectangle r : boxes) {
        g2d.setColor(bgColour);
        g2d.drawRect(r.x - 1, r.y - 1, r.width + 2, r.height + 2);
        g2d.drawRect(r.x + 1, r.y + 1, r.width - 2, r.height - 2);
        g2d.setColor(fgColour);
        g2d.drawRect(r.x, r.y, r.width, r.height);
    }

    return canvas;
}

From source file:DBMS.UpdateFileUpload.java

private static BufferedImage resizeImage(BufferedImage originalImage, int type) {
    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();//www.j  av  a2 s.com
    return resizedImage;
}

From source file:Utils.java

public static BufferedImage createGradientMask(int width, int height, int orientation) {
    // algorithm derived from Romain Guy's blog
    BufferedImage gradient = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = gradient.createGraphics();
    GradientPaint paint = new GradientPaint(0.0f, 0.0f, new Color(1.0f, 1.0f, 1.0f, 1.0f),
            orientation == SwingConstants.HORIZONTAL ? width : 0.0f,
            orientation == SwingConstants.VERTICAL ? height : 0.0f, new Color(1.0f, 1.0f, 1.0f, 0.0f));
    g.setPaint(paint);//from w  w w  .j a v  a2 s. co  m
    g.fill(new Rectangle2D.Double(0, 0, width, height));

    g.dispose();
    gradient.flush();

    return gradient;
}

From source file:com.pureinfo.srm.common.ImageHelper.java

public static void drawRectangle(Paint _color, Point _point, OutputStream _os) throws PureException {
    int nWidth = _point.x;
    int nHeight = _point.y;

    BufferedImage image = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    g2.setPaint(_color);/*from  ww w  .jav a 2  s  .com*/
    g2.fillRect(0, 0, nWidth, nHeight);

    g2.dispose();
    try {
        EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, _os);
    } catch (Exception ex) {
        throw new PureException(PureException.UNKNOWN, "", ex);
    }
}