2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import java.awt.event.ActionEvent;
22 import java.awt.RenderingHints;
23 import javax.imageio.ImageIO;
24 import org.jibble.epsgraphics.EpsGraphics2D;
25 import jalview.gui.EPSOptions;
26 import java.awt.Graphics2D;
28 import java.awt.image.BufferedImage;
29 import java.awt.Graphics;
34 public class ImageMaker
36 public static final int EPS = 0;
37 public static final int PNG = 1;
45 public ImageMaker(Component parent, int type, String title,
46 int width, int height, File file, String EPStitle)
52 JalviewFileChooser chooser;
53 chooser = type == EPS ? getEPSChooser() : getPNGChooser();
55 chooser.setFileView(new jalview.io.JalviewFileView());
56 chooser.setDialogTitle(title);
57 chooser.setToolTipText("Save");
59 int value = chooser.showSaveDialog(parent);
61 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
63 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
64 chooser.getSelectedFile().getParent());
66 file = chooser.getSelectedFile();
74 out = new FileOutputStream(file);
77 setupEPS(width, height, EPStitle);
79 setupPNG(width, height);
83 System.out.println("Error creating " + (type == EPS ? "EPS" : "PNG") +
89 public Graphics getGraphics()
95 void setupPNG(int width, int height)
97 bi = new BufferedImage(width, height,
98 BufferedImage.TYPE_INT_RGB);
99 graphics = bi.getGraphics();
100 Graphics2D ig2 = (Graphics2D) graphics;
101 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
102 RenderingHints.VALUE_ANTIALIAS_ON);
105 public void writeImage()
115 ImageIO.write(bi, "png", out);
122 ex.printStackTrace();
126 void setupEPS(int width, int height, String title)
128 boolean accurateText = true;
130 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
133 // If we need to prompt, and if the GUI is visible then
134 // Prompt for EPS rendering style
135 if (renderStyle.equalsIgnoreCase("Prompt each time")
137 (System.getProperty("java.awt.headless") != null
138 && System.getProperty("java.awt.headless").equals("true")))
140 EPSOptions eps = new EPSOptions();
141 renderStyle = eps.getValue();
143 if (renderStyle == null || eps.cancelled)
147 if (renderStyle.equalsIgnoreCase("text"))
149 accurateText = false;
153 pg = new EpsGraphics2D(title, out, 0, 0, width,
155 Graphics2D ig2 = (Graphics2D) pg;
156 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
157 RenderingHints.VALUE_ANTIALIAS_ON);
159 pg.setAccurateTextMode(accurateText);
163 catch (Exception ex) { }
167 JalviewFileChooser getPNGChooser()
169 return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
170 "LAST_DIRECTORY"), new String[]
173 {"Portable network graphics"},
174 "Portable network graphics");
177 JalviewFileChooser getEPSChooser()
179 return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
180 "LAST_DIRECTORY"), new String[]
183 {"Encapsulated Postscript"},
184 "Encapsulated Postscript");