2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.bin.Jalview;
24 import jalview.gui.EPSOptions;
25 import jalview.gui.SVGOptions;
26 import jalview.io.JalviewFileChooser;
28 import java.awt.Component;
29 import java.awt.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.RenderingHints;
32 import java.awt.image.BufferedImage;
34 import java.io.FileOutputStream;
36 import javax.imageio.ImageIO;
38 import org.jfree.graphics2d.svg.SVGGraphics2D;
39 import org.jfree.graphics2d.svg.SVGHints;
40 import org.jibble.epsgraphics.EpsGraphics2D;
42 public class ImageMaker
58 EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG(
59 "PNG", MessageManager.getString("label.png_image"),
60 getPNGChooser()), SVG("SVG", "SVG", getSVGChooser());
62 private JalviewFileChooser chooser;
68 TYPE(String name, String label, JalviewFileChooser chooser)
72 this.chooser = chooser;
75 public String getName()
80 public JalviewFileChooser getChooser()
85 public String getLabel()
93 public ImageMaker(Component parent, TYPE type, String title, int width,
94 int height, File file, String fileTitle)
100 JalviewFileChooser chooser;
101 chooser = type.getChooser();
102 chooser.setFileView(new jalview.io.JalviewFileView());
103 chooser.setDialogTitle(title);
104 chooser.setToolTipText(MessageManager.getString("action.save"));
105 int value = chooser.showSaveDialog(parent);
107 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
109 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
110 .getSelectedFile().getParent());
111 file = chooser.getSelectedFile();
119 out = new FileOutputStream(file);
120 if (type == TYPE.SVG)
122 setupSVG(width, height, fileTitle);
124 else if (type == TYPE.EPS)
126 setupEPS(width, height, fileTitle);
128 else if (type == TYPE.PNG)
130 setupPNG(width, height);
133 } catch (Exception ex)
135 System.out.println("Error creating "
136 + type.getName() + " file.");
141 public Graphics getGraphics()
148 public void writeImage()
159 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
160 out.write(svgData.getBytes());
165 ImageIO.write(bi, "png", out);
170 } catch (Exception ex)
172 ex.printStackTrace();
176 void setupEPS(int width, int height, String title)
178 boolean accurateText = true;
180 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
183 // If we need to prompt, and if the GUI is visible then
184 // Prompt for EPS rendering style
185 if (renderStyle.equalsIgnoreCase("Prompt each time")
186 && !(System.getProperty("java.awt.headless") != null && System
187 .getProperty("java.awt.headless").equals("true")))
189 EPSOptions eps = new EPSOptions();
190 renderStyle = eps.getValue();
192 if (renderStyle == null || eps.cancelled)
198 if (renderStyle.equalsIgnoreCase("text"))
200 accurateText = false;
205 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
207 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
208 RenderingHints.VALUE_ANTIALIAS_ON);
210 pg.setAccurateTextMode(accurateText);
213 } catch (Exception ex)
218 void setupPNG(int width, int height)
220 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
221 graphics = bi.getGraphics();
222 Graphics2D ig2 = (Graphics2D) graphics;
223 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
224 RenderingHints.VALUE_ANTIALIAS_ON);
228 void setupSVG(int width, int height, String title)
231 g2 = new SVGGraphics2D(width, height);
234 String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
237 // If we need to prompt, and if the GUI is visible then
238 // Prompt for EPS rendering style
239 if (renderStyle.equalsIgnoreCase("Prompt each time")
240 && !(System.getProperty("java.awt.headless") != null && System
241 .getProperty("java.awt.headless").equals("true")))
243 SVGOptions svgOption = new SVGOptions();
244 renderStyle = svgOption.getValue();
246 if (renderStyle == null || svgOption.cancelled)
252 if (renderStyle.equalsIgnoreCase("lineart"))
254 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
255 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
261 static JalviewFileChooser getPNGChooser()
263 if (Jalview.isHeadlessMode())
267 return new jalview.io.JalviewFileChooser(
268 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
269 { "png" }, new String[]
270 { "Portable network graphics" }, "Portable network graphics");
273 static JalviewFileChooser getEPSChooser()
275 if (Jalview.isHeadlessMode())
279 return new jalview.io.JalviewFileChooser(
280 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
281 { "eps" }, new String[]
282 { "Encapsulated Postscript" }, "Encapsulated Postscript");
285 static JalviewFileChooser getSVGChooser()
287 if (Jalview.isHeadlessMode())
291 return new jalview.io.JalviewFileChooser(
292 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
293 { "svg" }, new String[]
294 { "Scalable Vector Graphics" }, "Scalable Vector Graphics");