Example usage for java.awt Graphics dispose

List of usage examples for java.awt Graphics dispose

Introduction

In this page you can find the example usage for java.awt Graphics 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

/**
 * Smoothly scales the given {@link BufferedImage} to the given width and height using the
 * {@link Image#SCALE_SMOOTH} algorithm (generally bicubic resampling or bilinear filtering).
 *
 * @param source The source image./*from ww w  .j av  a2s  .  c o m*/
 * @param width  The destination width to scale to.
 * @param height The destination height to scale to.
 * @return A new, scaled image.
 */
public static BufferedImage scaledImage(BufferedImage source, int width, int height) {
    Image scaledImage = source.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    BufferedImage scaledBufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics g = scaledBufImage.createGraphics();
    g.drawImage(scaledImage, 0, 0, null);
    g.dispose();
    return scaledBufImage;
}

From source file:Main.java

/**
 * Quickly crops an image from its source.
 * @param src The source image./*www .j av  a 2  s  .co m*/
 * @param x The x coordinate to start at.
 * @param y The y coordinate to start at.
 * @param width The width to clip.
 * @param height The height to clip.
 * @return
 */
public static BufferedImage imgUtilFastCrop(BufferedImage src, int x, int y, int width, int height) {
    if (src == null)
        return null;
    if (x == 0 && y == 0 && width == src.getWidth() && height == src.getHeight())
        return imgUtilFastCopy(src);
    else {
        BufferedImage b = new BufferedImage(width, height, src.getType());
        Graphics g = b.getGraphics();
        g.drawImage(src, -x, -y, null);
        g.dispose();
        return b;
    }
}

From source file:Utils.java

/**
 * Produces a resized image that is of the given dimensions
 *
 * @param image The original image//from   ww w  .java2s . co m
 * @param width The desired width
 * @param height The desired height
 * @return The new BufferedImage
 */
public static BufferedImage scaledImage(BufferedImage image, int width, int height) {
    BufferedImage newImage = createCompatibleImage(width, height);
    Graphics graphics = newImage.createGraphics();

    graphics.drawImage(image, 0, 0, width, height, null);

    graphics.dispose();
    return newImage;
}

From source file:Main.java

/**
 * Creates (in a rather clumsy way) the font metrics for a given {@link Font}.
 * /*ww w  . j  a v a 2s  .  co m*/
 * @param aFont
 *          the font instance to create the font metrics for, cannot be
 *          <code>null</code>.
 * @return a font metrics, never <code>null</code>.
 */
private static FontMetrics createFontMetrics(final Font aFont) {
    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics canvas = img.getGraphics();

    try {
        return canvas.getFontMetrics(aFont);
    } finally {
        canvas.dispose();
        canvas = null;
        img = null;
    }
}

From source file:Main.java

/**
 * Create a custom cursor out of the specified image, with the specified hotspot.
 *//* w  w w.  j  a  v  a2  s  . c om*/
public static Cursor createImageCursor(Image img, Point hotspot) {
    Toolkit tk = Toolkit.getDefaultToolkit();

    // for now, just report the cursor restrictions, then blindly create
    int w = img.getWidth(null);
    int h = img.getHeight(null);
    Dimension d = tk.getBestCursorSize(w, h);
    //         int colors = tk.getMaximumCursorColors();
    //         Log.debug("Creating custom cursor [desiredSize=" + w + "x" + h +
    //                   ", bestSize=" + d.width + "x" + d.height +
    //                   ", maxcolors=" + colors + "].");

    // if the passed-in image is smaller, pad it with transparent pixels and use it anyway.
    if (((w < d.width) && (h <= d.height)) || ((w <= d.width) && (h < d.height))) {
        Image padder = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration().createCompatibleImage(d.width, d.height, Transparency.BITMASK);
        Graphics g = padder.getGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();

        // and reassign the image to the padded image
        img = padder;

        // and adjust the 'best' to cheat the hotspot checking code
        d.width = w;
        d.height = h;
    }

    // make sure the hotspot is valid
    if (hotspot == null) {
        hotspot = new Point(d.width / 2, d.height / 2);
    } else {
        hotspot.x = Math.min(d.width - 1, Math.max(0, hotspot.x));
        hotspot.y = Math.min(d.height - 1, Math.max(0, hotspot.y));
    }

    // and create the cursor
    return tk.createCustomCursor(img, hotspot, "samskivertDnDCursor");
}

From source file:edu.gmu.cs.sim.util.media.PDFEncoder.java

public static void generatePDF(Component component, File file) {
    int width = component.getWidth();
    int height = component.getHeight();
    try {// w w  w .ja  v a  2  s . c  om
        Document document = new Document(new com.lowagie.text.Rectangle(width, height));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.addAuthor("MASON");
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics g2 = tp.createGraphics(width, height, new DefaultFontMapper());
        component.paint(g2);
        g2.dispose();
        cb.addTemplate(tp, 0, 0);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ImageClip.java

/**
 * Clips the input image to the specified shape
 * // ww w  . j  a v  a2s  .  co  m
 * @param image
 *           the input image
 * @param clipVerts
 *           list of x, y pairs defining the clip shape, normalised
 *           to image dimensions (think texture coordinates)
 * @return The smallest image containing those pixels that fall
 *         inside the clip shape
 */
public static BufferedImage clip(BufferedImage image, float... clipVerts) {
    assert clipVerts.length >= 6;
    assert clipVerts.length % 2 == 0;

    int[] xp = new int[clipVerts.length / 2];
    int[] yp = new int[xp.length];

    int minX = image.getWidth(), minY = image.getHeight(), maxX = 0, maxY = 0;

    for (int j = 0; j < xp.length; j++) {
        xp[j] = Math.round(clipVerts[2 * j] * image.getWidth());
        yp[j] = Math.round(clipVerts[2 * j + 1] * image.getHeight());

        minX = Math.min(minX, xp[j]);
        minY = Math.min(minY, yp[j]);
        maxX = Math.max(maxX, xp[j]);
        maxY = Math.max(maxY, yp[j]);
    }

    for (int i = 0; i < xp.length; i++) {
        xp[i] -= minX;
        yp[i] -= minY;
    }

    Polygon clip = new Polygon(xp, yp, xp.length);
    BufferedImage out = new BufferedImage(maxX - minX, maxY - minY, image.getType());
    Graphics g = out.getGraphics();
    g.setClip(clip);

    g.drawImage(image, -minX, -minY, null);
    g.dispose();

    return out;
}

From source file:com.uksf.mf.core.utility.loaders.ImageLoad.java

/**
 * Changes colour of all non-transparent pixels for given image to given colour
 * @param image - image to change colours in
 * @param newColour colour to change to/* w  ww  .  j  a  v  a  2 s . co  m*/
 * @return image with changed colours
 */
private static ImageIcon changeImageColour(ImageIcon image, int newColour) {
    LogHandler.logSeverity(INFO, "Converting image: " + image + " colour to: " + newColour);
    BufferedImage bufferedImage = new BufferedImage(image.getIconWidth(), image.getIconHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = bufferedImage.createGraphics();
    image.paintIcon(null, graphics, 0, 0);
    graphics.dispose();
    for (int x = 0; x < bufferedImage.getWidth(); x++) {
        for (int y = 0; y < bufferedImage.getHeight(); y++) {
            int colour = bufferedImage.getRGB(x, y);
            colour = (((colour >> 24) & 0xff) << 24) | (((colour & 0x00ff0000) >> 16) << 16)
                    | (((colour & 0x0000ff00) >> 8) << 8) | (colour & 0x000000ff);
            if (colour != COLOUR_TRANSPARENT.getRGB()) {
                newColour = (((colour >> 24) & 0xff) << 24) | (((newColour & 0x00ff0000) >> 16) << 16)
                        | (((newColour & 0x0000ff00) >> 8) << 8) | (newColour & 0x000000ff);
                bufferedImage.setRGB(x, y, newColour);
            }
        }
    }
    return new ImageIcon(bufferedImage);
}

From source file:Utils.java

/** 
 * Renders a component into an image, which is useful for playing with the component's 
 * resultant image in special effects or transitions
 *
 * @param component The component to render
 * @return A buffered image with the rendered component. 
 *//* w  w  w . j  a v a2  s.c o m*/
public static BufferedImage renderComponentToImage(JComponent component) {
    //Create the image
    BufferedImage image = createCompatibleImage(component.getWidth(), component.getHeight());

    //Render the component onto the image
    Graphics graphics = image.createGraphics();
    //      component.update(graphics);
    component.paint(graphics);
    graphics.dispose();
    return image;
}

From source file:de.laures.cewolf.util.ImageHelper.java

public static BufferedImage loadBufferedImage(String fileName) {
    Image image = loadImage(fileName);
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }//ww w .j  a v  a  2s .  com
    /*        final boolean hasAlpha = hasAlpha(image);
    int transparency = Transparency.OPAQUE;
    if (hasAlpha) {
    transparency = Transparency.BITMASK;
    }*/
    int width = (int) Math.max(1.0, image.getWidth(null));
    int height = (int) Math.max(1.0, image.getHeight(null));
    // BufferedImage bimage = GRAPHICS_CONV.createCompatibleImage(width, height, transparency);
    BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    final Graphics g = bimage.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
}