List of usage examples for org.jfree.chart.encoders SunPNGEncoderAdapter encode
@Override public void encode(BufferedImage bufferedImage, OutputStream outputStream) throws IOException
From source file:bzstats.chart.AbstractChartFactory.java
/** * Utility function to save an image as a PNG file. * * @param image/* ww w. j av a2s .c o m*/ * The image to save. * @param filename * The filename to save the image as. * @throws BZStatsException * If the file could not be written. */ public static final void savePNGImage(BufferedImage image, String filename) throws BzStatsException { File outputfile = new File(filename); // overwrite if (outputfile.exists()) { if (outputfile.canWrite()) { outputfile.delete(); } else { throw new BzStatsException("Cannot overwrite " + filename); } } boolean filecreated; try { filecreated = outputfile.createNewFile(); } catch (IOException e2) { throw new BzStatsException("Failed to create file " + filename); } if (filecreated) { OutputStream out = null; try { out = new FileOutputStream(outputfile); } catch (FileNotFoundException e3) { throw new BzStatsException("Failed to write to file " + filename); } SunPNGEncoderAdapter encoder = new SunPNGEncoderAdapter(); try { encoder.encode(image, out); } catch (IOException e) { throw new BzStatsException("Failed to encode file " + filename); } finally { try { out.close(); } catch (IOException e1) { throw new BzStatsException("Failed to close file " + filename); } } } else { throw new BzStatsException("Failed to create file " + filename); } }
From source file:org.atomserver.testutils.plot.PerfPlotter.java
/** *//*w w w.j a v a 2 s .c om*/ public JFreeChart createPlot(XYDataset dataSet) { JFreeChart chart = createChart(dataSet); if (log.isTraceEnabled()) log.trace("chart= " + chart); try { java.awt.image.BufferedImage buffImage = chart.createBufferedImage(width, hieght); if (log.isTraceEnabled()) log.trace("buffImage= " + buffImage); org.jfree.chart.encoders.SunPNGEncoderAdapter pngEncoder = new org.jfree.chart.encoders.SunPNGEncoderAdapter(); java.io.FileOutputStream fileOS = new java.io.FileOutputStream(pngFileName); if (log.isTraceEnabled()) log.trace("fileOS= " + fileOS); pngEncoder.encode(buffImage, fileOS); fileOS.close(); if (log.isTraceEnabled()) log.trace("PNG file created sucessfully!!!!"); } catch (Exception ee) { log.error(ee); return null; } return chart; }