List of usage examples for org.jfree.chart.encoders ImageEncoder encode
public void encode(BufferedImage bufferedImage, OutputStream outputStream) throws IOException;
From source file:piramide.interaction.reasoner.FuzzyReasonerWizardFacade.java
@Override public void generateMembershipFunctionGraph(boolean isInput, boolean isDevices, String variableName, RegionDistributionInfo[] linguisticTerms, OutputStream destination, int width, int height, Geolocation geo, DecayFunctions decayFunction, Calendar when) { BufferedImage img;/*from w w w . j av a 2 s . c o m*/ if (variableName == null) { img = createErrorMessagesImage("Error generating graph: variableName not provided"); } else if (linguisticTerms == null) { img = createErrorMessagesImage("Error generating graph: linguisticTerms not provided"); } else if (isInput && isDevices && !isValidDeviceVariableName(variableName)) { img = createErrorMessagesImage("Error generating graph: invalid device variable name: " + variableName); } else if (isInput && !isDevices && !isValidUserVariableName(variableName)) { img = createErrorMessagesImage("Error generating graph: invalid user variable name: " + variableName); } else { try { final WarningStore warningStore = new WarningStore(); final net.sourceforge.jFuzzyLogic.rule.Variable variable = processVariable(isInput, isDevices, variableName, linguisticTerms, geo, decayFunction, when, warningStore); final JFreeChart theChart = variable.chart(false); final String[] messages = warningStore.getMessages(); if (messages.length > 0) { final Font font = TextTitle.DEFAULT_FONT; final Font bigBold = new Font(font.getName(), Font.BOLD, font.getSize() + 2); final Font bold = new Font(font.getName(), Font.BOLD, font.getSize()); theChart.addSubtitle(new TextTitle("WARNINGS:", bigBold, Color.RED, Title.DEFAULT_POSITION, Title.DEFAULT_HORIZONTAL_ALIGNMENT, Title.DEFAULT_VERTICAL_ALIGNMENT, Title.DEFAULT_PADDING)); for (String message : messages) theChart.addSubtitle(new TextTitle(message, bold, Color.RED, Title.DEFAULT_POSITION, Title.DEFAULT_HORIZONTAL_ALIGNMENT, Title.DEFAULT_VERTICAL_ALIGNMENT, Title.DEFAULT_PADDING)); } img = theChart.createBufferedImage(width, height); } catch (FuzzyReasonerException e) { e.printStackTrace(); img = createErrorMessagesImage("Error generating graph: " + e.getMessage()); } } try { final ImageEncoder myEncoder = ImageEncoderFactory.newInstance("png"); myEncoder.encode(img, destination); destination.flush(); destination.close(); } catch (IOException e) { // Cry e.printStackTrace(); return; } }
From source file:ch.agent.crnickl.demo.stox.Chart.java
private void saveChartAsPNG(JFreeChart chart, String fileName, int width, int height) throws KeyedException { OutputStream out = null;/* w w w . j a v a 2 s. co m*/ try { out = new BufferedOutputStream(new FileOutputStream(fileName)); BufferedImage bufferedImage = chart.createBufferedImage(width, height, null); ImageEncoder imageEncoder = ImageEncoderFactory.newInstance("png"); imageEncoder.encode(bufferedImage, out); } catch (Exception e) { throw K.JFC_OUTPUT_ERR.exception(e, fileName); } finally { try { if (out != null) out.close(); } catch (Exception e) { throw new RuntimeException(e); } } }