2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
21 import javax.imageio.*;
24 import java.awt.image.*;
26 import org.jibble.epsgraphics.*;
30 public class ImageMaker
32 public static final int EPS = 0;
34 public static final int PNG = 1;
46 public ImageMaker(Component parent, int type, String title, int width,
47 int height, File file, String EPStitle)
53 JalviewFileChooser chooser;
54 chooser = type == EPS ? getEPSChooser() : getPNGChooser();
56 chooser.setFileView(new jalview.io.JalviewFileView());
57 chooser.setDialogTitle(title);
58 chooser.setToolTipText("Save");
60 int value = chooser.showSaveDialog(parent);
62 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
64 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
65 .getSelectedFile().getParent());
67 file = chooser.getSelectedFile();
75 out = new FileOutputStream(file);
79 setupEPS(width, height, EPStitle);
83 setupPNG(width, height);
85 } catch (Exception ex)
87 System.out.println("Error creating "
88 + (type == EPS ? "EPS" : "PNG") + " file.");
93 public Graphics getGraphics()
98 void setupPNG(int width, int height)
100 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
101 graphics = bi.getGraphics();
102 Graphics2D ig2 = (Graphics2D) graphics;
103 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
104 RenderingHints.VALUE_ANTIALIAS_ON);
107 public void writeImage()
118 ImageIO.write(bi, "png", out);
122 } catch (Exception ex)
124 ex.printStackTrace();
128 void setupEPS(int width, int height, String title)
130 boolean accurateText = true;
132 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
135 // If we need to prompt, and if the GUI is visible then
136 // Prompt for EPS rendering style
137 if (renderStyle.equalsIgnoreCase("Prompt each time")
138 && !(System.getProperty("java.awt.headless") != null && System
139 .getProperty("java.awt.headless").equals("true")))
141 EPSOptions eps = new EPSOptions();
142 renderStyle = eps.getValue();
144 if (renderStyle == null || eps.cancelled)
150 if (renderStyle.equalsIgnoreCase("text"))
152 accurateText = false;
157 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
158 Graphics2D ig2 = (Graphics2D) pg;
159 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
160 RenderingHints.VALUE_ANTIALIAS_ON);
162 pg.setAccurateTextMode(accurateText);
165 } catch (Exception ex)
170 JalviewFileChooser getPNGChooser()
172 return new jalview.io.JalviewFileChooser(
173 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
174 { "png" }, new String[]
175 { "Portable network graphics" }, "Portable network graphics");
178 JalviewFileChooser getEPSChooser()
180 return new jalview.io.JalviewFileChooser(
181 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
182 { "eps" }, new String[]
183 { "Encapsulated Postscript" }, "Encapsulated Postscript");