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:com.aurel.track.attachment.AttachBL.java

private static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }//from  w  w  w .  ja  va 2 s . c o  m

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see Determining If an Image Has Transparent Pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:Bouncer.java

protected void render() {
    Graphics g = getGraphics();/*from  w w w .ja va  2  s . c  o  m*/
    if (g != null) {
        Dimension d = getSize();
        if (checkImage(d)) {
            Graphics imageGraphics = image.getGraphics();

            imageGraphics.setColor(getBackground());
            imageGraphics.fillRect(0, 0, d.width, d.height);
            imageGraphics.setColor(getForeground());

            paint(imageGraphics);

            g.drawImage(image, 0, 0, null);

            imageGraphics.dispose();
        }
        g.dispose();
    }
}

From source file:edu.ku.brc.specify.utilapps.ERDTable.java

public void paint(Graphics g) {
    if (!ERDVisualizer.isDoShadow()) {
        super.paint(g);

    } else {// ww  w.j av a  2  s  . c  o m
        if (shadowBuffer == null) {
            getBackgroundImageBuffer();
            Graphics gg = shadowBuffer.createGraphics();
            //gg.translate(5, 5);
            inner.paint(gg);
            gg.dispose();
        }

        g.drawImage(shadowBuffer, 0, 0, null);
    }

}

From source file:org.apache.pdfbox.rendering.TestPDFToImage.java

/**
 * Create an image; the part between the smaller and the larger image is painted black, the rest
 * in white/*ww  w.ja v  a2  s  . co  m*/
 *
 * @param minWidth width of the smaller image
 * @param minHeight width of the smaller image
 * @param maxWidth height of the larger image
 * @param maxHeight height of the larger image
 *
 * @return
 */
private BufferedImage createEmptyDiffImage(int minWidth, int minHeight, int maxWidth, int maxHeight) {
    BufferedImage bim3 = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_RGB);
    Graphics graphics = bim3.getGraphics();
    if (minWidth != maxWidth || minHeight != maxHeight) {
        graphics.setColor(Color.BLACK);
        graphics.fillRect(0, 0, maxWidth, maxHeight);
    }
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, minWidth, minHeight);
    graphics.dispose();
    return bim3;
}

From source file:com.limegroup.gnutella.gui.notify.AnimatedWindow.java

private void prepareAnimation(Point location) {
    if (animationImage != null) {
        // already have an image, this could be the case if a hide animation
        // is started while a show animation is still in progress or the
        // other//from w w  w .ja  va2  s.c  om
        // way around
        return;
    }

    // reset window to original size to capture all content
    pack();
    Dimension size = getSize();

    if (mode.needsBackgroundImage() && backgroundImage == null) {
        try {
            Robot robot = new Robot();
            backgroundImage = robot.createScreenCapture(new Rectangle(location, size));
        } catch (AWTException e) {
            LOG.warn("Could not capture background image", e);
            backgroundImage = null;
        }
    }

    animationImage = getGraphicsConfiguration().createCompatibleImage(size.width, size.height);
    Graphics grahpics = animationImage.getGraphics();
    getContentPane().paint(grahpics);
    grahpics.dispose();
}

From source file:org.squale.squaleweb.applicationlayer.action.export.ppt.PPTData.java

private BufferedImage convertImgToBufferedImg(Image limage, String l) {
    if (limage instanceof BufferedImage) {
        return ((BufferedImage) limage);
    } else {/*from   w w  w .  j a v a  2  s  . c o  m*/
        Image lImage = new ImageIcon(limage).getImage();
        BufferedImage bufferedimage = new BufferedImage(lImage.getWidth(null), lImage.getHeight(null),
                BufferedImage.TYPE_INT_RGB);
        Graphics gr = bufferedimage.createGraphics();
        gr.drawImage(lImage, 0, 0, null);
        gr.dispose();
        return (bufferedimage);
    }
}

From source file:tilt.image.Picture.java

/**
 * Convert from the original png file to greyscale png. Save original.
 * @throws ImageException //from ww w.j  a  v a2 s  .  co  m
 */
void convertToGreyscale() throws ImageException {
    try {
        BufferedImage png = ImageIO.read(orig);
        BufferedImage grey = new BufferedImage(png.getWidth(), png.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Graphics g = grey.getGraphics();
        g.drawImage(png, 0, 0, null);
        g.dispose();
        if (!isWholePicture()) {
            // clear excluded regions
            Graphics2D g2d = grey.createGraphics();
            g2d.setColor(Color.white);
            if (coords[0][0].doubleValue() > 0.0) {
                int w = grey.getWidth();
                int h = grey.getHeight();
                g2d.fillRect(0, 0, getPropVal(coords[0][0], w), h);
            }
            if (coords[1][0].doubleValue() < 100.0) {
                int x = getPropVal(coords[1][0], grey.getWidth());
                g2d.fillRect(x, 0, grey.getWidth() - x, grey.getHeight());
            }
            if (coords[2][1].doubleValue() < 100.0) {
                int y = getPropVal(coords[2][1], grey.getHeight());
                g2d.fillRect(0, y, grey.getWidth(), grey.getHeight() - y);
            }
            if (coords[0][1].doubleValue() > 0.0) {
                int y = getPropVal(coords[0][1], grey.getHeight());
                g2d.fillRect(0, 0, grey.getWidth(), y);
            }
            g2d.dispose();
        }
        greyscale = File.createTempFile(PictureRegistry.PREFIX, PictureRegistry.SUFFIX);
        ImageIO.write(grey, "png", greyscale);
    } catch (Exception e) {
        throw new ImageException(e);
    }
}

From source file:org.deegree.ogcwebservices.wms.dataaccess.ID2PInterpolation.java

private void writeErrorMessage() {
    Graphics g = image.getGraphics();
    g.setColor(Color.WHITE);/*from  w w  w.ja  v a 2  s.c  om*/
    g.fillRect(0, 0, getMap.getWidth(), getMap.getHeight());
    g.setColor(Color.RED);
    g.drawString("not enough values for interpolation available", 10, 50);
    g.dispose();
}

From source file:org.sbs.util.ImageCompress.java

/**
 * gif/*from w w  w  .ja  v a  2s . c om*/
 * 
 * @param originalFile
 *            
 * @param resizedFile
 *            ?
 * @param newWidth
 *            
 * @param newHeight
 *             -1?
 * @param quality
 *             ()
 * @throws IOException
 */
public void resize(File originalFile, File resizedFile, int newWidth, int newHeight, float quality)
        throws IOException {
    if (quality < 0 || quality > 1) {
        throw new IllegalArgumentException("Quality has to be between 0 and 1");
    }
    ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
    Image i = ii.getImage();
    Image resizedImage = null;
    int iWidth = i.getWidth(null);
    int iHeight = i.getHeight(null);
    if (newHeight == -1) {
        if (iWidth > iHeight) {
            resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth, Image.SCALE_SMOOTH);
        } else {
            resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth, Image.SCALE_SMOOTH);
        }
    } else {
        resizedImage = i.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
    }
    // This code ensures that all the pixels in the image are loaded.
    Image temp = new ImageIcon(resizedImage).getImage();
    // Create the buffered image.
    BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    // Copy image to buffered image.
    Graphics g = bufferedImage.createGraphics();
    // Clear background and paint the image.
    g.setColor(Color.white);
    g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
    g.drawImage(temp, 0, 0, null);
    g.dispose();
    // Soften.
    float softenFactor = 0.05f;
    float[] softenArray = { 0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0,
            softenFactor, 0 };
    Kernel kernel = new Kernel(3, 3, softenArray);
    ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
    bufferedImage = cOp.filter(bufferedImage, null);
    // Write the jpeg to a file.
    FileOutputStream out = FileUtils.openOutputStream(resizedFile);
    // Encodes image as a JPEG data stream
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
    param.setQuality(quality, true);
    encoder.setJPEGEncodeParam(param);
    encoder.encode(bufferedImage);
}

From source file:edu.ku.brc.specify.rstools.GoogleEarthExporter.java

/**
 * @param outputFile//from   ww  w . j av a 2  s .c  o m
 * @param defaultIconFile
 */
protected void createKMZ(final File outputFile, final File defaultIconFile) throws IOException {
    // now we have the KML in outputFile
    // we need to create a KMZ (zip file containing doc.kml and other files)

    // create a buffer for reading the files
    byte[] buf = new byte[1024];
    int len;

    // create the KMZ file
    File outputKMZ = File.createTempFile("sp6-export-", ".kmz");
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputKMZ));

    // add the doc.kml file to the ZIP
    FileInputStream in = new FileInputStream(outputFile);
    // add ZIP entry to output stream
    out.putNextEntry(new ZipEntry("doc.kml"));
    // copy the bytes
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    // complete the entry
    out.closeEntry();
    in.close();

    // add a "files" directory to the KMZ file
    ZipEntry filesDir = new ZipEntry("files/");
    out.putNextEntry(filesDir);
    out.closeEntry();

    if (defaultIconFile != null) {
        File iconTmpFile = defaultIconFile;
        if (false) {
            // Shrink File
            ImageIcon icon = new ImageIcon(defaultIconFile.getAbsolutePath());
            BufferedImage bimage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                    BufferedImage.TYPE_INT_ARGB);
            Graphics g = bimage.createGraphics();
            g.drawImage(icon.getImage(), 0, 0, null);
            g.dispose();
            BufferedImage scaledBI = GraphicsUtils.getScaledInstance(bimage, 16, 16, true);
            iconTmpFile = File.createTempFile("sp6-export-icon-scaled", ".png");
            ImageIO.write(scaledBI, "PNG", iconTmpFile);
        }

        // add the specify32.png file (default icon file) to the ZIP (in the "files" directory)
        in = new FileInputStream(iconTmpFile);
        // add ZIP entry to output stream
        out.putNextEntry(new ZipEntry("files/specify32.png"));
        // copy the bytes
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // complete the entry
        out.closeEntry();
        in.close();
    }

    // complete the ZIP file
    out.close();

    // now put the KMZ file where the KML output was
    FileUtils.copyFile(outputKMZ, outputFile);

    outputKMZ.delete();
}