Example usage for org.jfree.chart.encoders EncoderUtil writeBufferedImage

List of usage examples for org.jfree.chart.encoders EncoderUtil writeBufferedImage

Introduction

In this page you can find the example usage for org.jfree.chart.encoders EncoderUtil writeBufferedImage.

Prototype

public static void writeBufferedImage(BufferedImage image, String format, OutputStream outputStream,
        boolean encodeAlpha) throws IOException 

Source Link

Document

Encode the image in a specific format and write it to an OutputStream.

Usage

From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

public static void writeChartToJPEG(JFreeChart chart, ChartRenderingInfo info, int width, int height,
        File fileName, int resolution) throws IOException {
    // Background color
    Paint saved = chart.getBackgroundPaint();
    if (((Color) saved).getAlpha() == 0) {
        chart.setBackgroundPaint(Color.WHITE);
        chart.setBackgroundImageAlpha(255);
        if (chart.getLegend() != null)
            chart.getLegend().setBackgroundPaint(Color.WHITE);
        // legends and stuff
        for (int i = 0; i < chart.getSubtitleCount(); i++)
            if (PaintScaleLegend.class.isAssignableFrom(chart.getSubtitle(i).getClass()))
                ((PaintScaleLegend) chart.getSubtitle(i)).setBackgroundPaint(Color.WHITE);

        // apply bg
        chart.getPlot().setBackgroundPaint(Color.WHITE);
    }/*w w w  .  jav a  2s .  co  m*/
    //
    if (resolution == 72)
        writeChartToJPEG(chart, width, height, fileName);
    else {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
        try {
            BufferedImage image = paintScaledChartToBufferedImage(chart, info, out, width, height, resolution,
                    BufferedImage.TYPE_INT_RGB);
            EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, 1.f);
        } finally {
            out.close();
        }
    }
}