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:TexturedPanel.java

/**
 * Creates a new TexturePaint using the provided icon.
 *///from w  w w. j  ava  2  s .c  o m
private void setupIconPainter(Icon texture) {
    if (texture == null) {
        ourPainter = null;
        return;
    }

    int w = texture.getIconWidth();
    int h = texture.getIconHeight();

    if (w <= 0 || h <= 0) {
        ourPainter = null;
        return;
    }

    BufferedImage buff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();
    texture.paintIcon(this, g2, 0, 0);
    ourPainter = new TexturePaint(buff, new Rectangle(0, 0, w, h));

    g2.dispose();
}

From source file:IconDemoApp.java

/**
 * Resizes an image using a Graphics2D object backed by a BufferedImage.
 * //from  w w  w. j  a  v a2s  .co m
 * @param srcImg -
 *            source image to scale
 * @param w -
 *            desired width
 * @param h -
 *            desired height
 * @return - the new resized image
 */
private Image getScaledImage(Image srcImg, int w, int h) {
    BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = resizedImg.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(srcImg, 0, 0, w, h, null);
    g2.dispose();
    return resizedImg;
}

From source file:com.datamyne.charts.FinallyDemo.java

/**
 * Creates PDf file.// w  w w  .java  2 s. co  m
 * @param outputStream {@link OutputStream}.
 * @throws DocumentException
 * @throws IOException
 */
public void create(OutputStream outputStream) throws DocumentException, IOException {
    Document document = null;
    PdfWriter writer = null;

    try {
        //instantiate document and writer
        document = new Document();
        writer = PdfWriter.getInstance(document, outputStream);

        //open document
        document.open();

        //get dummy text
        String text = getText();
        //create text font
        com.itextpdf.text.Font font = new com.itextpdf.text.Font(FontFamily.TIMES_ROMAN, 10.0f);

        //add text before
        document.add(new Paragraph(new Chunk(text, font)));

        //add image
        int width = 300;
        int height = 300;
        JFreeChart chart = getChart();

        //create PdfContentByte
        //if you work with this object, you write to
        //the top most layer, meaning anything behind
        //will be clipped
        PdfContentByte contentByte = writer.getDirectContent();
        //create PdfTemplate from PdfContentByte
        PdfTemplate template = contentByte.createTemplate(width, height);
        //create Graphics2D from PdfTemplate
        Graphics2D g2 = template.createGraphics(width, height, new DefaultFontMapper());
        //setup the drawing area
        Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);
        //pass the Graphics2D and drawing area to JFreeChart
        chart.draw(g2, r2D, null);
        g2.dispose(); //always dispose this

        //create Image from PdfTemplate
        Image image = Image.getInstance(template);
        document.add(image);

        //add text after
        document.add(new Paragraph(new Chunk(text, font)));

        //release resources
        document.close();
        document = null;

        writer.close();
        writer = null;
    } catch (DocumentException de) {
        throw de;
    } finally {
        //release resources
        if (null != document) {
            try {
                document.close();
            } catch (Exception ex) {
            }
        }

        if (null != writer) {
            try {
                writer.close();
            } catch (Exception ex) {
            }
        }
    }
}

From source file:doge.photo.DogePhotoManipulator.java

private void render(BufferedImage sourceImage, BufferedImage destinationImage) {
    Graphics2D destinationGraphics = destinationImage.createGraphics();
    try {/* w ww  .ja va  2  s.  com*/
        setGraphicsHints(destinationGraphics);
        renderBackground(sourceImage, destinationImage, destinationGraphics);
        renderOverlay(destinationImage, destinationGraphics);
    } finally {
        destinationGraphics.dispose();
    }
}

From source file:com.funambol.foundation.util.MediaUtils.java

/**
 * Creates the thumbnail.// ww  w .j av a  2  s .c o m
 *
 * @param imageFile the image file
 * @param thumbFile the empty thumbnail file
 * @param thumbX the width of the thumbnail
 * @param thumbY the height of the thumbnail
 * @param imageName the image file name with extension
 * @param tolerance the percentage of tolerance before creating a thumbnail
 * @return true is the thumbnail has been created, false otherwise
 * @throws IOException if an error occurs
 */
private static boolean createThumbnail(File imageFile, File thumbFile, int thumbX, int thumbY, String imageName,
        double tolerance) throws IOException {

    FileInputStream fileis = null;
    ImageInputStream imageis = null;

    Iterator readers = null;

    try {

        readers = ImageIO.getImageReadersByFormatName(imageName.substring(imageName.lastIndexOf('.') + 1));
        if (readers == null || (!readers.hasNext())) {
            throw new IOException("File not supported");
        }

        ImageReader reader = (ImageReader) readers.next();

        fileis = new FileInputStream(imageFile);
        imageis = ImageIO.createImageInputStream(fileis);
        reader.setInput(imageis, true);

        // Determines thumbnail height, width and quality
        int thumbWidth = thumbX;
        int thumbHeight = thumbY;

        double thumbRatio = (double) thumbWidth / (double) thumbHeight;
        int imageWidth = reader.getWidth(0);
        int imageHeight = reader.getHeight(0);

        //
        // Don't create the thumbnail if the original file is smaller
        // than required size increased by % tolerance
        //
        if (imageWidth <= (thumbWidth * (1 + tolerance / 100))
                && imageHeight <= (thumbHeight * (1 + tolerance / 100))) {

            return false;
        }

        double imageRatio = (double) imageWidth / (double) imageHeight;
        if (thumbRatio < imageRatio) {
            thumbHeight = (int) (thumbWidth / imageRatio);
        } else {
            thumbWidth = (int) (thumbHeight * imageRatio);
        }

        ImageReadParam param = reader.getDefaultReadParam();
        param.setSourceSubsampling(3, 3, 0, 0);

        BufferedImage bi = reader.read(0, param);

        Image thumb = bi.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH);

        BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(thumb, 0, 0, thumbWidth, thumbHeight, null);

        FileOutputStream fileOutputStream = new FileOutputStream(thumbFile);
        ImageIO.write(thumbImage, "jpg", fileOutputStream);

        thumb.flush();
        thumbImage.flush();
        fileOutputStream.flush();
        fileOutputStream.close();
        graphics2D.dispose();

    } finally {
        if (fileis != null) {
            fileis.close();
        }
        if (imageis != null) {
            imageis.close();
        }
    }

    return true;
}

From source file:net.sf.jasperreports.charts.util.ImageChartRendererFactory.java

@Override
public Renderable getRenderable(JasperReportsContext jasperReportsContext, JFreeChart chart,
        ChartHyperlinkProvider chartHyperlinkProvider, Rectangle2D rectangle) {
    int dpi = JRPropertiesUtil.getInstance(jasperReportsContext)
            .getIntegerProperty(Renderable.PROPERTY_IMAGE_DPI, 72);
    double scale = dpi / 72d;

    BufferedImage bi = new BufferedImage((int) (scale * (int) rectangle.getWidth()),
            (int) (scale * rectangle.getHeight()), BufferedImage.TYPE_INT_ARGB);

    List<JRPrintImageAreaHyperlink> areaHyperlinks = null;

    Graphics2D grx = bi.createGraphics();
    try {/*from w ww  . j  ava  2s.com*/
        grx.scale(scale, scale);

        if (chartHyperlinkProvider != null && chartHyperlinkProvider.hasHyperlinks()) {
            areaHyperlinks = ChartUtil.getImageAreaHyperlinks(chart, chartHyperlinkProvider, grx, rectangle);
        } else {
            chart.draw(grx, rectangle);
        }
    } finally {
        grx.dispose();
    }

    try {
        return new SimpleDataRenderer(
                JRImageLoader.getInstance(jasperReportsContext).loadBytesFromAwtImage(bi, ImageTypeEnum.PNG),
                areaHyperlinks);
    } catch (JRException e) {
        throw new JRRuntimeException(e);
    }
}

From source file:com.kahlon.guard.controller.PersonImageManager.java

private 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;
}

From source file:Main.java

public void drawImage() {
    Graphics2D g = img.createGraphics();
    RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHints(hints);/*  w w w  .  j a  v a  2  s  . c  o m*/

    g.setColor(Color.RED);
    int x = (int) mouse.getX();
    int y = (int) mouse.getY();
    g.setStroke(new BasicStroke(2));
    int s = 3;
    g.drawLine(x - s, y, x + s, y);
    g.drawLine(x, y - s, x, y + s);
    l.setIcon(new ImageIcon(img));

    g.dispose();
}

From source file:com.kahlon.guard.controller.PersonImageManager.java

private BufferedImage resizeImageWithHint(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();
    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:com.sire.web.CajFacturaEnviadaBean.java

private void savePicture() {
    if (file != null) {
        try {/*from  ww  w . ja v  a 2s .co m*/
            BufferedImage originalImage = ImageIO.read(file.getInputstream());

            BufferedImage outputImage = new BufferedImage((int) (originalImage.getWidth() * 0.25),
                    (int) (originalImage.getHeight() * 0.25), originalImage.getType());

            Graphics2D g2d = outputImage.createGraphics();
            g2d.drawImage(originalImage, 0, 0, (int) (originalImage.getWidth() * 0.25),
                    (int) (originalImage.getHeight() * 0.25), null);
            g2d.dispose();

            String imagesFolder = System.getProperty("imagesFolder");

            if (imagesFolder == null) {
                String currentUsersHomeDir = System.getProperty("user.home");
                imagesFolder = currentUsersHomeDir + File.separator + "photos";
            }

            Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
            ImageWriter writer = (ImageWriter) iter.next();
            ImageWriteParam iwp = writer.getDefaultWriteParam();

            iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            float quality = 1.0f; // reduce quality by 0%  
            iwp.setCompressionQuality(quality);

            File f = new File(imagesFolder + File.separator + fileName);
            try (FileImageOutputStream output = new FileImageOutputStream(f)) {
                writer.setOutput(output);

                IIOImage image = new IIOImage(outputImage, null, null);
                writer.write(null, image, iwp);
                writer.dispose();
            }
        } catch (IOException ex) {
            LOGGER.severe(ex.getMessage());
        }
    }
}