package jalview.util; import java.awt.event.ActionEvent; import java.awt.RenderingHints; import javax.imageio.ImageIO; import org.jibble.epsgraphics.EpsGraphics2D; import jalview.gui.EPSOptions; import java.awt.Graphics2D; import java.io.*; import java.awt.image.BufferedImage; import java.awt.Graphics; import jalview.io.*; import java.awt.*; public class ImageMaker { public static final int EPS = 0; public static final int PNG = 1; int type = -1; EpsGraphics2D pg; Graphics graphics; FileOutputStream out; BufferedImage bi; public ImageMaker(Component parent, int type, String title, int width, int height, File file, String EPStitle) { this.type = type; if (file == null) { JalviewFileChooser chooser; chooser = type == EPS ? getEPSChooser() : getPNGChooser(); chooser.setFileView(new jalview.io.JalviewFileView()); chooser.setDialogTitle(title); chooser.setToolTipText("Save"); int value = chooser.showSaveDialog(parent); if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION) { jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser.getSelectedFile().getParent()); file = chooser.getSelectedFile(); } } if(file!=null) { try { out = new FileOutputStream(file); if (type == EPS) setupEPS(width, height, EPStitle); else setupPNG(width, height); } catch (Exception ex) { System.out.println("Error creating " + (type == EPS ? "EPS" : "PNG") + " file."); } } } public Graphics getGraphics() { return graphics; } void setupPNG(int width, int height) { bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); graphics = bi.getGraphics(); Graphics2D ig2 = (Graphics2D) graphics; ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } public void writeImage() { try{ switch(type) { case EPS: pg.flush(); pg.close(); break; case PNG: ImageIO.write(bi, "png", out); out.close(); break; } } catch (Exception ex) { ex.printStackTrace(); } } void setupEPS(int width, int height, String title) { boolean accurateText = true; String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING", "Prompt each time"); // If we need to prompt, and if the GUI is visible then // Prompt for EPS rendering style if (renderStyle.equalsIgnoreCase("Prompt each time") && ! (System.getProperty("java.awt.headless") != null && System.getProperty("java.awt.headless").equals("true"))) { EPSOptions eps = new EPSOptions(); renderStyle = eps.getValue(); if (renderStyle == null || eps.cancelled) return; } if (renderStyle.equalsIgnoreCase("text")) { accurateText = false; } try { pg = new EpsGraphics2D(title, out, 0, 0, width, height); Graphics2D ig2 = (Graphics2D) pg; ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); pg.setAccurateTextMode(accurateText); graphics = pg; } catch (Exception ex) { } } JalviewFileChooser getPNGChooser() { return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty( "LAST_DIRECTORY"), new String[] {"png"}, new String[] {"Portable network graphics"}, "Portable network graphics"); } JalviewFileChooser getEPSChooser() { return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty( "LAST_DIRECTORY"), new String[] {"eps"}, new String[] {"Encapsulated Postscript"}, "Encapsulated Postscript"); } }