2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.util;
\r
22 import javax.imageio.*;
\r
25 import java.awt.image.*;
\r
27 import org.jibble.epsgraphics.*;
\r
28 import jalview.gui.*;
\r
29 import jalview.io.*;
\r
31 public class ImageMaker
\r
33 public static final int EPS = 0;
\r
34 public static final int PNG = 1;
\r
39 FileOutputStream out;
\r
42 public ImageMaker(Component parent, int type, String title,
\r
43 int width, int height, File file, String EPStitle)
\r
49 JalviewFileChooser chooser;
\r
50 chooser = type == EPS ? getEPSChooser() : getPNGChooser();
\r
52 chooser.setFileView(new jalview.io.JalviewFileView());
\r
53 chooser.setDialogTitle(title);
\r
54 chooser.setToolTipText("Save");
\r
56 int value = chooser.showSaveDialog(parent);
\r
58 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
\r
60 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
\r
61 chooser.getSelectedFile().getParent());
\r
63 file = chooser.getSelectedFile();
\r
71 out = new FileOutputStream(file);
\r
75 setupEPS(width, height, EPStitle);
\r
79 setupPNG(width, height);
\r
82 catch (Exception ex)
\r
84 System.out.println("Error creating " + (type == EPS ? "EPS" : "PNG") +
\r
90 public Graphics getGraphics()
\r
95 void setupPNG(int width, int height)
\r
97 bi = new BufferedImage(width, height,
\r
98 BufferedImage.TYPE_INT_RGB);
\r
99 graphics = bi.getGraphics();
\r
100 Graphics2D ig2 = (Graphics2D) graphics;
\r
101 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
102 RenderingHints.VALUE_ANTIALIAS_ON);
\r
105 public void writeImage()
\r
116 ImageIO.write(bi, "png", out);
\r
121 catch (Exception ex)
\r
123 ex.printStackTrace();
\r
127 void setupEPS(int width, int height, String title)
\r
129 boolean accurateText = true;
\r
131 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
\r
132 "Prompt each time");
\r
134 // If we need to prompt, and if the GUI is visible then
\r
135 // Prompt for EPS rendering style
\r
136 if (renderStyle.equalsIgnoreCase("Prompt each time")
\r
138 (System.getProperty("java.awt.headless") != null
\r
139 && System.getProperty("java.awt.headless").equals("true")))
\r
141 EPSOptions eps = new EPSOptions();
\r
142 renderStyle = eps.getValue();
\r
144 if (renderStyle == null || eps.cancelled)
\r
150 if (renderStyle.equalsIgnoreCase("text"))
\r
152 accurateText = false;
\r
157 pg = new EpsGraphics2D(title, out, 0, 0, width,
\r
159 Graphics2D ig2 = (Graphics2D) pg;
\r
160 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
161 RenderingHints.VALUE_ANTIALIAS_ON);
\r
163 pg.setAccurateTextMode(accurateText);
\r
167 catch (Exception ex)
\r
171 JalviewFileChooser getPNGChooser()
\r
173 return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
174 "LAST_DIRECTORY"), new String[]
\r
177 {"Portable network graphics"},
\r
178 "Portable network graphics");
\r
181 JalviewFileChooser getEPSChooser()
\r
183 return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
184 "LAST_DIRECTORY"), new String[]
\r
187 {"Encapsulated Postscript"},
\r
188 "Encapsulated Postscript");
\r