Example usage for java.awt Graphics2D dispose

List of usage examples for java.awt Graphics2D dispose

Introduction

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

Prototype

public abstract void dispose();

Source Link

Document

Disposes of this graphics context and releases any system resources that it is using.

Usage

From source file:Main.java

/**
 * Shrinks an image to fit into memory more
 * Effectively./*from ww w .  j  a  v  a2  s .  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

private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type) {
    int IMG_WIDTH = 512;
    int IMG_CLAHEIGHT = 512;
    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_CLAHEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_CLAHEIGHT, null);
    g.dispose();
    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

/**
 * Resize image to specified dimensions/*from   w  w w . ja  v a2 s  .  c om*/
 * @param image image to resize
 * @param width new image width
 * @param height new image height
 * @return resized image
 */
public static BufferedImage resizeImage(BufferedImage image, int width, int height) {
    BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();

    return resizedImage;
}

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();
    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

/**
 * Changes the opacity of a given image.
 * @param src The source image./*from   w  w  w.  ja  v a 2s.c o  m*/
 * @param opacity The opacity to change to.
 * @return
 */
public static BufferedImage imgUtilAdjustImageTransparency(BufferedImage src, float opacity) {
    if (src == null)
        return null;
    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity);
    BufferedImage cpy = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) cpy.getGraphics();
    g.setComposite(ac);
    g.drawImage(src, 0, 0, null);
    g.dispose();
    return cpy;
}

From source file:Main.java

public static BufferedImage asCompatibleImage(Image img, int transparency) {
    BufferedImage ret = defaultScreenDeviceConfiguration().createCompatibleImage(img.getWidth(null),
            img.getHeight(null), transparency);
    Graphics2D gc = ret.createGraphics();
    gc.setComposite(AlphaComposite.Src);
    gc.drawImage(img, 0, 0, null);//w  w  w .j  a va  2 s . c  o m
    gc.dispose();
    return ret;
}

From source file:Main.java

/**
 * Creates a buffered image for the given parameters. If there is not enough
 * memory to create the image then a OutOfMemoryError is thrown.
 *//* w  ww  . j av a  2s  .c  om*/
public static BufferedImage createBufferedImage(int w, int h, Color background) {
    BufferedImage result = null;

    if (w > 0 && h > 0) {
        int type = (background != null) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
        result = new BufferedImage(w, h, type);

        // Clears background
        if (background != null) {
            Graphics2D g2 = result.createGraphics();
            clearRect(g2, new Rectangle(w, h), background);
            g2.dispose();
        }
    }

    return result;
}

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);//  w  w w  .j a  v  a  2  s  . com
    g.setComposite(AlphaComposite.SrcAtop);
    g.setColor(color);
    g.fillRect(0, 0, w, h);
    g.dispose();
    return dyed;
}

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  www  .  j av  a  2 s  .co m*/
    g2.fillRect(0, 0, nWidth, nHeight);

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

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();
    return resizedImage;
}