2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 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
21 import java.awt.RenderingHints;
\r
22 import javax.imageio.ImageIO;
\r
23 import org.jibble.epsgraphics.EpsGraphics2D;
\r
24 import jalview.gui.EPSOptions;
\r
25 import java.awt.Graphics2D;
\r
27 import java.awt.image.BufferedImage;
\r
28 import java.awt.Graphics;
\r
29 import jalview.io.*;
\r
33 public class ImageMaker
\r
35 public static final int EPS = 0;
\r
36 public static final int PNG = 1;
\r
41 FileOutputStream out;
\r
44 public ImageMaker(Component parent, int type, String title,
\r
45 int width, int height, File file, String EPStitle)
\r
51 JalviewFileChooser chooser;
\r
52 chooser = type == EPS ? getEPSChooser() : getPNGChooser();
\r
54 chooser.setFileView(new jalview.io.JalviewFileView());
\r
55 chooser.setDialogTitle(title);
\r
56 chooser.setToolTipText("Save");
\r
58 int value = chooser.showSaveDialog(parent);
\r
60 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
\r
62 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
\r
63 chooser.getSelectedFile().getParent());
\r
65 file = chooser.getSelectedFile();
\r
73 out = new FileOutputStream(file);
\r
76 setupEPS(width, height, EPStitle);
\r
78 setupPNG(width, height);
\r
80 catch (Exception ex)
\r
82 System.out.println("Error creating " + (type == EPS ? "EPS" : "PNG") +
\r
88 public Graphics getGraphics()
\r
94 void setupPNG(int width, int height)
\r
96 bi = new BufferedImage(width, height,
\r
97 BufferedImage.TYPE_INT_RGB);
\r
98 graphics = bi.getGraphics();
\r
99 Graphics2D ig2 = (Graphics2D) graphics;
\r
100 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
101 RenderingHints.VALUE_ANTIALIAS_ON);
\r
104 public void writeImage()
\r
114 ImageIO.write(bi, "png", out);
\r
119 catch (Exception ex)
\r
121 ex.printStackTrace();
\r
125 void setupEPS(int width, int height, String title)
\r
127 boolean accurateText = true;
\r
129 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
\r
130 "Prompt each time");
\r
132 // If we need to prompt, and if the GUI is visible then
\r
133 // Prompt for EPS rendering style
\r
134 if (renderStyle.equalsIgnoreCase("Prompt each time")
\r
136 (System.getProperty("java.awt.headless") != null
\r
137 && System.getProperty("java.awt.headless").equals("true")))
\r
139 EPSOptions eps = new EPSOptions();
\r
140 renderStyle = eps.getValue();
\r
142 if (renderStyle == null || eps.cancelled)
\r
146 if (renderStyle.equalsIgnoreCase("text"))
\r
148 accurateText = false;
\r
152 pg = new EpsGraphics2D(title, out, 0, 0, width,
\r
154 Graphics2D ig2 = (Graphics2D) pg;
\r
155 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
156 RenderingHints.VALUE_ANTIALIAS_ON);
\r
158 pg.setAccurateTextMode(accurateText);
\r
162 catch (Exception ex) { }
\r
166 JalviewFileChooser getPNGChooser()
\r
168 return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
169 "LAST_DIRECTORY"), new String[]
\r
172 {"Portable network graphics"},
\r
173 "Portable network graphics");
\r
176 JalviewFileChooser getEPSChooser()
\r
178 return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
179 "LAST_DIRECTORY"), new String[]
\r
182 {"Encapsulated Postscript"},
\r
183 "Encapsulated Postscript");
\r