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:edu.cudenver.bios.chartsvc.representation.LegendImageRepresentation.java

/**
 * Called internally by Restlet library to write the image as the HTTP
 * response.//from  w  w w.  ja v  a  2  s.  c  o m
 * @param out output stream
 */
@Override
public void write(OutputStream out) throws IOException {
    // build the legend from the plot, and write it to a jpeg image
    if (plot != null) {
        LegendTitle legend = new LegendTitle(plot, new ColumnArrangement(), new ColumnArrangement());
        legend.setFrame(BlockBorder.NONE);
        //legend.setMargin(new RectangleInsets(2.0, 2.0, 2.0, 2.0));
        legend.setBackgroundPaint(Color.white);
        legend.setPosition(RectangleEdge.BOTTOM);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        Rectangle2D.Double legendArea = new Rectangle2D.Double(0, 0, width, height);
        g.clip(legendArea);
        legend.arrange(g, new RectangleConstraint(width, height));
        legend.draw(g, legendArea);
        g.dispose();
        EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out);
    }
}

From source file:domainhealth.frontend.graphics.JFreeChartGraphImpl.java

/**
 * Check to see if current graph has empty chart lines (ie. no data-series
 * in any lines), and if so, write the text 'NO DATA' in large letters 
 * across the front of the graph//from   w  w  w .j  a va  2 s . com
 * 
 * @param graphImage The current image representation of the generated graph
 */
@SuppressWarnings("unchecked")
private void addNoDataLogoIfEmpty(BufferedImage graphImage) {
    int maxStatCount = 0;
    List<XYSeries> seriesList = xySeriesCollection.getSeries();

    for (XYSeries series : seriesList) {
        maxStatCount = Math.max(maxStatCount, series.getItemCount());
    }

    if (maxStatCount <= 0) {
        Graphics2D graphics2D = get2DGraphics(graphImage);
        graphics2D.setFont(new Font(GRAPH_TEXT_FONT, Font.PLAIN, 36));
        graphics2D.drawString(NO_DATA_TEXT, 200, 210);
        graphics2D.dispose();
    } else if (maxStatCount <= 1) {
        Graphics2D graphics2D = get2DGraphics(graphImage);
        graphics2D.setFont(new Font(GRAPH_TEXT_FONT, Font.PLAIN, 22));
        graphics2D.drawString(WAITING_FOR_DATA_TEXT_LN1, 152, 205);
        graphics2D.dispose();
        graphics2D = get2DGraphics(graphImage);
        graphics2D.setFont(new Font(GRAPH_TEXT_FONT, Font.PLAIN, 15));
        graphics2D.drawString(WAITING_FOR_DATA_TEXT_LN2, 81, 225);
        graphics2D.dispose();
    }
}

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

public void draw(final Graphics2D graphics2D, final Rectangle2D bounds) {
    this.bounds = (Rectangle2D) bounds.clone();
    if (chartRenderingInfo != null) {
        this.chartRenderingInfo.clear();
    }/*  w ww . j a va2 s.  c om*/
    final Graphics2D g2 = (Graphics2D) graphics2D.create();
    this.chart.draw(g2, bounds, chartRenderingInfo);
    g2.dispose();

    if (chartRenderingInfo == null || debugRendering == false) {
        return;
    }

    graphics2D.setColor(Color.RED);
    final Rectangle2D dataArea = getDataAreaOffset();
    final EntityCollection entityCollection = chartRenderingInfo.getEntityCollection();
    for (int i = 0; i < entityCollection.getEntityCount(); i++) {
        final ChartEntity chartEntity = entityCollection.getEntity(i);
        if (chartEntity instanceof XYItemEntity || chartEntity instanceof CategoryItemEntity
                || chartEntity instanceof PieSectionEntity) {
            final Area a = new Area(chartEntity.getArea());
            if (buggyDrawArea) {
                a.transform(AffineTransform.getTranslateInstance(dataArea.getX(), dataArea.getY()));
            }
            a.intersect(new Area(dataArea));
            graphics2D.draw(a);
        } else {
            graphics2D.draw(chartEntity.getArea());
        }
    }
}

From source file:WeeklyReport.Sections.Declines.java

public Image declinesByReasonImage(PdfWriter writer) throws BadElementException {
    JFreeChart chart = declinesByReasonChart();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(600f, 400f);
    Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper());
    Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f);
    chart.draw(graphics2d, rectangle2d);
    graphics2d.dispose();

    Image chartImage = Image.getInstance(template);
    return chartImage;
}

From source file:WeeklyReport.Sections.Declines.java

public Image declinesByCommodityImage(PdfWriter writer) throws BadElementException {
    JFreeChart chart = new Declines().declinesByCommodityClassChart();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createAppearance(600f, 400f);
    Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper());
    Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f);
    chart.draw(graphics2d, rectangle2d);
    graphics2d.dispose();

    Image chartImage = Image.getInstance(template);
    return chartImage;
}

From source file:de.romankreisel.faktotum.beans.BundesbruderBean.java

public void rotateProfilePictureClockwise(BundesbruderEntity bundesbruder, double angle) throws IOException {
    BufferedImage image = this.getImageFromByteArray(bundesbruder.getPictureOriginal());
    double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    int w = image.getWidth(), h = image.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
    GraphicsConfiguration gc = image.createGraphics().getDeviceConfiguration();
    BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
    Graphics2D g = result.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(angle, w / 2, h / 2);/*from w w  w . j a va  2 s.  c o  m*/
    g.drawRenderedImage(image, null);
    g.dispose();
    this.setProfilePicture(bundesbruder, this.storeImageToByteArray(image));
}

From source file:msi.gama.outputs.layers.charts.ChartJFreeChartOutput.java

@Override
public BufferedImage getImage(final int sizeX, final int sizeY, final boolean antiAlias) {
    if (!ready) {
        return cache;
    }//from www .  ja v a  2 s  .c o  m
    if (antiAlias != oldAntiAlias) {
        oldAntiAlias = antiAlias;
        getJFChart().setAntiAlias(antiAlias);
        getJFChart().setTextAntiAlias(antiAlias);
    }
    final Graphics2D g2D = getGraphics(sizeX, sizeY);
    chart.draw(g2D, r, info);
    g2D.dispose();
    return cache;

}

From source file:weka.core.ChartUtils.java

/**
 * Render a combined histogram and box plot chart from summary data
 * //from w w w.  j  a v  a  2s.  c  o m
 * @param width the width of the resulting image
 * @param height the height of the resulting image
 * @param bins the values for the chart
 * @param freqs the corresponding frequencies
 * @param summary the summary stats for the box plot
 * @param outliers an optional list of outliers for the box plot
 * @param additionalArgs optional arguments to the renderer (may be null)
 * @return a buffered image
 * @throws Exception if a problem occurs
 */
public static BufferedImage renderCombinedBoxPlotAndHistogramFromSummaryData(int width, int height,
        List<String> bins, List<Double> freqs, List<Double> summary, List<Double> outliers,
        List<String> additionalArgs) throws Exception {

    String plotTitle = "Combined Chart";
    String userTitle = getOption(additionalArgs, "-title");
    plotTitle = (userTitle != null) ? userTitle : plotTitle;

    List<String> opts = new ArrayList<String>();
    opts.add("-title=histogram");
    BufferedImage hist = renderHistogramFromSummaryData(width / 2, height, bins, freqs, opts);
    opts.clear();
    opts.add("-title=box plot");
    BufferedImage box = null;
    try {
        box = renderBoxPlotFromSummaryData(width / 2, height, summary, outliers, opts);
    } catch (Exception ex) {
        // if we can't generate the box plot (i.e. probably because
        // data is 100% missing) then just return the histogram
    }

    if (box == null) {
        width /= 2;
    }
    BufferedImage img = new BufferedImage(width, height + 20, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = img.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
    g2d.setFont(new Font("SansSerif", Font.BOLD, 12));
    g2d.setColor(Color.lightGray);
    g2d.fillRect(0, 0, width, height + 20);
    g2d.setColor(Color.black);
    FontMetrics fm = g2d.getFontMetrics();
    int fh = fm.getHeight();
    int sw = fm.stringWidth(plotTitle);

    if (box != null) {
        g2d.drawImage(box, 0, 20, null);
        g2d.drawImage(hist, width / 2 + 1, 20, null);
    } else {
        g2d.drawImage(hist, 0, 20, null);
    }
    g2d.drawString(plotTitle, width / 2 - sw / 2, fh + 2);
    g2d.dispose();

    return img;
}

From source file:D20140128.ApacheXMLGraphicsTest.TilingPatternExample.java

/**
 * Default constructor./*from   ww  w  .  j a va 2  s.c om*/
 */
public TilingPatternExample() {
    //Created TexturePaint instance
    this.tile = new BufferedImage(40, 20, BufferedImage.TYPE_INT_RGB);
    Graphics2D tileg2d = tile.createGraphics();
    tileg2d.setBackground(Color.WHITE);
    tileg2d.clearRect(0, 0, tile.getWidth(), tile.getHeight());
    tileg2d.setColor(Color.BLUE);
    tileg2d.fillOval(2, 2, tile.getWidth() - 2, tile.getHeight() - 2);
    tileg2d.dispose();
    Rectangle2D rect = new Rectangle2D.Double(2, 2, tile.getWidth() / 2.0, tile.getHeight() / 2.0);
    this.paint = new TexturePaint(tile, rect);
}

From source file:ScreenCapture.java

public boolean crop() {
    if (startPoint.equals(endPoint))
        return true;

    boolean succeeded = true;

    int x1 = (startPoint.x < endPoint.x) ? startPoint.x : endPoint.x;
    int y1 = (startPoint.y < endPoint.y) ? startPoint.y : endPoint.y;

    int x2 = (startPoint.x > endPoint.x) ? startPoint.x : endPoint.x;
    int y2 = (startPoint.y > endPoint.y) ? startPoint.y : endPoint.y;

    int width = (x2 - x1) + 1;
    int height = (y2 - y1) + 1;

    BufferedImage biCrop = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = biCrop.createGraphics();
    BufferedImage bi = (BufferedImage) image;
    BufferedImage bi2 = bi.getSubimage(x1, y1, width, height);
    g2d.drawImage(bi2, null, 0, 0);//from   w w w.j  a v  a 2 s. co m

    g2d.dispose();

    if (succeeded)
        setImage(biCrop);
    else {
        startPoint.x = endPoint.x;
        startPoint.y = endPoint.y;
        repaint();
    }

    return succeeded;
}