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:ddf.catalog.transformer.input.tika.TikaInputTransformer.java

private void createThumbnail(InputStream input, Metacard metacard) {
    try {/*from w  w  w  .  java  2 s. c o m*/
        Image image = ImageIO.read(new CloseShieldInputStream(input));

        if (null != image) {
            BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = bufferedImage.createGraphics();
            graphics.drawImage(image, null, null);
            graphics.dispose();

            BufferedImage thumb = Scalr.resize(bufferedImage, 200);

            try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                ImageIO.write(thumb, "jpeg", out);

                byte[] thumbBytes = out.toByteArray();
                metacard.setAttribute(new AttributeImpl(Metacard.THUMBNAIL, thumbBytes));
            }
        } else {
            LOGGER.warn("Unable to read image from input stream to create thumbnail.");
        }
    } catch (Exception e) {
        LOGGER.warn("Unable to read image from input stream to create thumbnail.", e);
    }
}

From source file:GraphEditorDemo.java

/**
 * copy the visible part of the graph to a file as a jpeg image
 * @param file/*  w  w  w . j av  a  2 s. c  o  m*/
 */
public void writeJPEGImage(File file) {
    int width = vv.getWidth();
    int height = vv.getHeight();

    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = bi.createGraphics();
    vv.paint(graphics);
    graphics.dispose();

    try {
        ImageIO.write(bi, "jpeg", file);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:de.fau.amos.ChartToPDF.java

/**
 * Converts Chart into PDF//from   ww w.  j a va2 s.  c  om
 * 
 * @param chart JFreeChart from CharRenderer that will be displayed in the PDF file
 * @param fileName Name of created PDF-file
 */
public void convertChart(JFreeChart chart, String fileName) {
    if (chart != null) {

        //set page size
        float width = PageSize.A4.getWidth();
        float height = PageSize.A4.getHeight();

        //creating a new pdf document
        Document document = new Document(new Rectangle(width, height));
        PdfWriter writer;
        try {
            writer = PdfWriter.getInstance(document,
                    new FileOutputStream(new File(System.getProperty("userdir.location"), fileName + ".pdf")));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        }

        document.addAuthor("Green Energy Cockpit");
        document.open();

        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);

        //sets the pdf page
        Graphics2D g2D = new PdfGraphics2D(tp, width, height);
        Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);

        //draws the passed JFreeChart into the PDF file
        chart.draw(g2D, r2D);

        g2D.dispose();
        cb.addTemplate(tp, 0, 0);

        document.close();

        writer.flush();
        writer.close();
    }
}

From source file:ca.sqlpower.wabit.report.ChartRenderer.java

public boolean renderReportContent(Graphics2D g, double width, double height, double scaleFactor, int pageIndex,
        boolean printing, SPVariableResolver variablesContext) {

    if (printing) {
        // If we're printing a streaming query, we have to
        // print whatever's displayed.
        if (this.chartCache == null || (this.chartCache != null && !this.chartCache.getQuery().isStreaming())) {
            refresh(false);/* w  w  w. ja  va2  s .c  o m*/
        }
    } else if (needsRefresh || this.chartCache == null) {
        // No chart loaded. Doing a refresh will trigger a new 
        // redraw later on.
        refresh();
        return false;
    }

    JFreeChart jFreeChart = null;
    try {
        jFreeChart = ChartSwingUtil.createChartFromQuery(chartCache);
        if (jFreeChart == null) {
            g.drawString("Loading...", 0, g.getFontMetrics().getHeight());
            return false;
        }

        Rectangle2D area = new Rectangle2D.Double(0, 0, width, height);

        // first pass establishes rendering info but draws nothing
        ChartRenderingInfo info = new ChartRenderingInfo();
        Graphics2D dummyGraphics = (Graphics2D) g.create(0, 0, 0, 0);
        jFreeChart.draw(dummyGraphics, area, info);
        dummyGraphics.dispose();

        // now for real
        Rectangle2D plotArea = info.getPlotInfo().getDataArea();
        ChartGradientPainter.paintChartGradient(g, area, (int) plotArea.getMaxY());
        jFreeChart.draw(g, area);

    } catch (Exception e) {
        logger.error("Error while rendering chart", e);
        g.drawString("Could not render chart: " + e.getMessage(), 0, g.getFontMetrics().getHeight());
    }
    return false;
}

From source file:net.rptools.lib.image.ThumbnailManager.java

private Image createThumbnail(File file) throws IOException {
    // Gather info
    File thumbnailFile = getThumbnailFile(file);
    if (thumbnailFile.exists()) {
        return ImageUtil.getImage(thumbnailFile);
    }//from   w  w  w  . jav  a  2  s .  co  m

    Image image = ImageUtil.getImage(file);
    Dimension imgSize = new Dimension(image.getWidth(null), image.getHeight(null));

    // Test if we Should we bother making a thumbnail ?
    // Jamz: New size 100k (was 30k) and put in check so we're not creating thumbnails LARGER than the original...
    if (file.length() < 102400
            || (imgSize.width <= thumbnailSize.width && imgSize.height <= thumbnailSize.height)) {
        return image;
    }
    // Transform the image
    SwingUtil.constrainTo(imgSize, Math.min(image.getWidth(null), thumbnailSize.width),
            Math.min(image.getHeight(null), thumbnailSize.height));
    BufferedImage thumbnailImage = new BufferedImage(imgSize.width, imgSize.height,
            ImageUtil.pickBestTransparency(image));

    Graphics2D g = thumbnailImage.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.drawImage(image, 0, 0, imgSize.width, imgSize.height, null);
    g.dispose();

    // Use png to preserve transparency
    FileUtils.writeByteArrayToFile(thumbnailFile, ImageUtil.imageToBytes(thumbnailImage, "png"));

    return thumbnailImage;
}

From source file:com.t3.image.ThumbnailManager.java

private Image createThumbnail(File file) throws IOException {

    // Gather info
    File thumbnailFile = getThumbnailFile(file);
    if (thumbnailFile.exists()) {
        return ImageUtil.getImage(thumbnailFile);
    }//w  w  w.  jav  a  2 s. com

    Image image = ImageUtil.getImage(file);

    // Should we bother making a thumbnail ?
    if (file.length() < 30 * 1024) {
        return image;
    }

    // Transform the image
    Dimension imgSize = new Dimension(image.getWidth(null), image.getHeight(null));
    SwingUtil.constrainTo(imgSize, thumbnailSize.width, thumbnailSize.height);

    BufferedImage thumbnailImage = new BufferedImage(imgSize.width, imgSize.height,
            ImageUtil.pickBestTransparency(image));

    Graphics2D g = thumbnailImage.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.drawImage(image, 0, 0, imgSize.width, imgSize.height, null);
    g.dispose();

    // Ensure path exists
    if (thumbnailFile.exists())
        thumbnailFile.delete();
    else
        thumbnailFile.getParentFile().mkdirs();

    try (OutputStream os = new FileOutputStream(thumbnailFile)) {
        IOUtils.write(ImageUtil.imageToBytes(thumbnailImage, "png"), os);
    }

    return thumbnailImage;
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    if (myImage != null) {
        int x = (getWidth() - myImage.getWidth()) / 2;
        int y = (getHeight() - myImage.getHeight()) / 2;
        g2d.drawImage(myImage, x, y, this);

        g2d.setColor(Color.RED);//from   ww  w.ja  va 2  s .  co m
        g2d.translate(x, y);
        g2d.draw(myOffice);
    }
    g2d.dispose();
}

From source file:de.chott.jfreechartsample.service.ChartService.java

/**
 * Schreibt mehrere JFreeCharts in ein PDF. Fr jedes Chart wird hierbei eine halbe PDF-Seite verwendet.
 * /*  www  .j  ava  2  s .  c o  m*/
 * @param charts
 * @return Das PDF als ByteArray
 */
private byte[] writeChartsToDocument(JFreeChart... charts) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        float width = PageSize.A4.getWidth();
        float height = PageSize.A4.getHeight() / 2;
        int index = 0;

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        document.open();
        PdfContentByte contentByte = writer.getDirectContent();

        for (JFreeChart chart : charts) {

            PdfTemplate template = contentByte.createTemplate(width, height);
            Graphics2D graphics2D = template.createGraphics(width, height);
            Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);

            chart.draw(graphics2D, rectangle2D);

            graphics2D.dispose();
            contentByte.addTemplate(template, 0, height - (height * index));
            index++;
        }

        writer.flush();
        document.close();

        return baos.toByteArray();
    } catch (Exception ex) {
        Logger.getLogger(ChartService.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setColor(getBackground());//from  w  w w.j av a  2 s . c o  m
    g2d.fillRect(0, 0, getWidth(), getHeight());
    if (image != null) {
        int x = getWidth() - image.getWidth();
        int y = getHeight() - image.getHeight();
        g2d.drawImage(image, x, y, this);
    }
    super.paintComponent(g2d);
    g2d.dispose();
}

From source file:org.pentaho.plugin.jfreereport.reportcharts.JFreeChartReportDrawable.java

/**
 * Returns an optional image-map for the entry.
 *
 * @param bounds the bounds for which the image map is computed.
 * @return the computed image-map or null if there is no image-map available.
 *//*from   w  ww  . java2s.c  om*/
public ImageMap getImageMap(final Rectangle2D bounds) {
    if (chartRenderingInfo == null) {
        return null;
    }
    final Rectangle2D dataArea = getDataAreaOffset();
    final Rectangle2D otherArea = new Rectangle2D.Double();

    if ((ObjectUtilities.equal(bounds, this.bounds)) == false) {
        final BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
        final Graphics2D graphics = image.createGraphics();
        draw(graphics, bounds);
        graphics.dispose();
    }

    final ImageMap map = new ImageMap();
    final EntityCollection entityCollection = chartRenderingInfo.getEntityCollection();
    final int count = entityCollection.getEntityCount();
    for (int i = 0; i < count; i++) {
        final ChartEntity chartEntity = entityCollection.getEntity(i);
        final Shape area = chartEntity.getArea();
        final String hrefValue = chartEntity.getURLText();
        final String tooltipValue = chartEntity.getToolTipText();
        if (StringUtils.isEmpty(tooltipValue) == false || StringUtils.isEmpty(hrefValue) == false) {
            final AbstractImageMapEntry entry;
            if (chartEntity instanceof XYItemEntity || chartEntity instanceof CategoryItemEntity
                    || chartEntity instanceof PieSectionEntity) {
                entry = createMapEntry(area, dataArea);
            } else {
                entry = createMapEntry(area, otherArea);
            }
            if (entry == null) {
                continue;
            }
            if (StringUtils.isEmpty(hrefValue) == false) {
                entry.setAttribute(LibXmlInfo.XHTML_NAMESPACE, "href", hrefValue);
            } else {
                entry.setAttribute(LibXmlInfo.XHTML_NAMESPACE, "href", "#");
            }
            if (StringUtils.isEmpty(tooltipValue) == false) {
                entry.setAttribute(LibXmlInfo.XHTML_NAMESPACE, "title", tooltipValue);
            }
            map.addMapEntry(entry);
        }
    }

    return map;
}