2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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
22 import javax.imageio.*;
25 import java.awt.image.*;
27 import org.jibble.epsgraphics.*;
31 public class ImageMaker
33 public static final int EPS = 0;
35 public static final int PNG = 1;
47 public ImageMaker(Component parent, int type, String title, int width,
48 int height, File file, String EPStitle)
54 JalviewFileChooser chooser;
55 chooser = type == EPS ? getEPSChooser() : getPNGChooser();
57 chooser.setFileView(new jalview.io.JalviewFileView());
58 chooser.setDialogTitle(title);
59 chooser.setToolTipText("Save");
61 int value = chooser.showSaveDialog(parent);
63 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
65 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
66 .getSelectedFile().getParent());
68 file = chooser.getSelectedFile();
76 out = new FileOutputStream(file);
80 setupEPS(width, height, EPStitle);
84 setupPNG(width, height);
86 } catch (Exception ex)
88 System.out.println("Error creating "
89 + (type == EPS ? "EPS" : "PNG") + " file.");
94 public Graphics getGraphics()
99 void setupPNG(int width, int height)
101 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
102 graphics = bi.getGraphics();
103 Graphics2D ig2 = (Graphics2D) graphics;
104 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
105 RenderingHints.VALUE_ANTIALIAS_ON);
108 public void writeImage()
119 ImageIO.write(bi, "png", out);
123 } catch (Exception ex)
125 ex.printStackTrace();
129 void setupEPS(int width, int height, String title)
131 boolean accurateText = true;
133 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
136 // If we need to prompt, and if the GUI is visible then
137 // Prompt for EPS rendering style
138 if (renderStyle.equalsIgnoreCase("Prompt each time")
139 && !(System.getProperty("java.awt.headless") != null && System
140 .getProperty("java.awt.headless").equals("true")))
142 EPSOptions eps = new EPSOptions();
143 renderStyle = eps.getValue();
145 if (renderStyle == null || eps.cancelled)
151 if (renderStyle.equalsIgnoreCase("text"))
153 accurateText = false;
158 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
159 Graphics2D ig2 = (Graphics2D) pg;
160 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
161 RenderingHints.VALUE_ANTIALIAS_ON);
163 pg.setAccurateTextMode(accurateText);
166 } catch (Exception ex)
171 JalviewFileChooser getPNGChooser()
173 return new jalview.io.JalviewFileChooser(jalview.bin.Cache
174 .getProperty("LAST_DIRECTORY"), new String[]
175 { "png" }, new String[]
176 { "Portable network graphics" }, "Portable network graphics");
179 JalviewFileChooser getEPSChooser()
181 return new jalview.io.JalviewFileChooser(jalview.bin.Cache
182 .getProperty("LAST_DIRECTORY"), new String[]
183 { "eps" }, new String[]
184 { "Encapsulated Postscript" }, "Encapsulated Postscript");