public File addImageToCache( BufferedImage image, JiraWorkflow workflow, int stepId, String width, String height, boolean showLabels, boolean maintainAspect) throws Exception { File file = getCacheFile(workflow, stepId, width, height, showLabels, maintainAspect); File baseDir = new File(file.getParent()); if (!baseDir.exists()) { baseDir.mkdirs(); } mxPngEncodeParam param = mxPngEncodeParam.getDefaultEncodeParam(image); FileOutputStream outputStream = new FileOutputStream(file); try { mxPngImageEncoder encoder = new mxPngImageEncoder(outputStream, param); encoder.encode(image); } finally { IOUtils.closeQuietly(outputStream); } return file; }
/** Saves XML+PNG format. */ protected void saveXmlPng(TrackSchemeFrame frame, String filename, Color bg) throws IOException { final mxGraphComponent graphComponent = trackScheme.getGUI().graphComponent; final mxGraph graph = trackScheme.getGraph(); // Creates the image for the PNG file BufferedImage image = mxCellRenderer.createBufferedImage( graph, null, 1, bg, graphComponent.isAntiAlias(), null, graphComponent.getCanvas()); // Creates the URL-encoded XML data mxCodec codec = new mxCodec(); String xml = URLEncoder.encode(mxXmlUtils.getXml(codec.encode(graph.getModel())), "UTF-8"); mxPngEncodeParam param = mxPngEncodeParam.getDefaultEncodeParam(image); param.setCompressedText(new String[] {"mxGraphModel", xml}); // Saves as a PNG file FileOutputStream outputStream = new FileOutputStream(new File(filename)); try { mxPngImageEncoder encoder = new mxPngImageEncoder(outputStream, param); if (image != null) { encoder.encode(image); } else { JOptionPane.showMessageDialog(graphComponent, "No Image Data"); } } finally { outputStream.close(); } }