1 package jalview.util;
\r
3 import java.awt.event.ActionEvent;
\r
4 import java.awt.RenderingHints;
\r
5 import javax.imageio.ImageIO;
\r
6 import org.jibble.epsgraphics.EpsGraphics2D;
\r
7 import jalview.gui.EPSOptions;
\r
8 import java.awt.Graphics2D;
\r
10 import java.awt.image.BufferedImage;
\r
11 import java.awt.Graphics;
\r
12 import jalview.io.*;
\r
16 public class ImageMaker
\r
18 public static final int EPS = 0;
\r
19 public static final int PNG = 1;
\r
24 FileOutputStream out;
\r
27 public ImageMaker(Component parent, int type, String title,
\r
28 int width, int height, File file, String EPStitle)
\r
34 JalviewFileChooser chooser;
\r
35 chooser = type == EPS ? getEPSChooser() : getPNGChooser();
\r
37 chooser.setFileView(new jalview.io.JalviewFileView());
\r
38 chooser.setDialogTitle(title);
\r
39 chooser.setToolTipText("Save");
\r
41 int value = chooser.showSaveDialog(parent);
\r
43 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
\r
45 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
\r
46 chooser.getSelectedFile().getParent());
\r
48 file = chooser.getSelectedFile();
\r
56 out = new FileOutputStream(file);
\r
59 setupEPS(width, height, EPStitle);
\r
61 setupPNG(width, height);
\r
63 catch (Exception ex)
\r
65 System.out.println("Error creating " + (type == EPS ? "EPS" : "PNG") +
\r
71 public Graphics getGraphics()
\r
77 void setupPNG(int width, int height)
\r
79 bi = new BufferedImage(width, height,
\r
80 BufferedImage.TYPE_INT_RGB);
\r
81 graphics = bi.getGraphics();
\r
82 Graphics2D ig2 = (Graphics2D) graphics;
\r
83 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
84 RenderingHints.VALUE_ANTIALIAS_ON);
\r
87 public void writeImage()
\r
97 ImageIO.write(bi, "png", out);
\r
102 catch (Exception ex)
\r
104 ex.printStackTrace();
\r
108 void setupEPS(int width, int height, String title)
\r
110 boolean accurateText = true;
\r
112 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
\r
113 "Prompt each time");
\r
115 // If we need to prompt, and if the GUI is visible then
\r
116 // Prompt for EPS rendering style
\r
117 if (renderStyle.equalsIgnoreCase("Prompt each time")
\r
119 (System.getProperty("java.awt.headless") != null
\r
120 && System.getProperty("java.awt.headless").equals("true")))
\r
122 EPSOptions eps = new EPSOptions();
\r
123 renderStyle = eps.getValue();
\r
125 if (renderStyle == null || eps.cancelled)
\r
129 if (renderStyle.equalsIgnoreCase("text"))
\r
131 accurateText = false;
\r
135 pg = new EpsGraphics2D(title, out, 0, 0, width,
\r
137 Graphics2D ig2 = (Graphics2D) pg;
\r
138 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
139 RenderingHints.VALUE_ANTIALIAS_ON);
\r
141 pg.setAccurateTextMode(accurateText);
\r
145 catch (Exception ex) { }
\r
149 JalviewFileChooser getPNGChooser()
\r
151 return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
152 "LAST_DIRECTORY"), new String[]
\r
155 {"Portable network graphics"},
\r
156 "Portable network graphics");
\r
159 JalviewFileChooser getEPSChooser()
\r
161 return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
162 "LAST_DIRECTORY"), new String[]
\r
165 {"Encapsulated Postscript"},
\r
166 "Encapsulated Postscript");
\r