2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.gui.EPSOptions;
24 import jalview.io.JalviewFileChooser;
26 import java.awt.Component;
27 import java.awt.Graphics;
28 import java.awt.Graphics2D;
29 import java.awt.RenderingHints;
30 import java.awt.image.BufferedImage;
32 import java.io.FileOutputStream;
34 import javax.imageio.ImageIO;
36 import org.jfree.graphics2d.svg.SVGGraphics2D;
37 import org.jibble.epsgraphics.EpsGraphics2D;
39 public class ImageMaker
55 EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG(
56 "PNG", MessageManager.getString("label.png_image"),
57 getPNGChooser()), SVG("SVG", "SVG", getSVGChooser());
59 private JalviewFileChooser chooser;
65 TYPE(String name, String label, JalviewFileChooser chooser)
69 this.chooser = chooser;
72 public String getName()
77 public JalviewFileChooser getChooser()
82 public String getLabel()
90 public ImageMaker(Component parent, TYPE type, String title, int width,
91 int height, File file, String fileTitle)
97 JalviewFileChooser chooser;
98 chooser = type.getChooser();
99 chooser.setFileView(new jalview.io.JalviewFileView());
100 chooser.setDialogTitle(title);
101 chooser.setToolTipText(MessageManager.getString("action.save"));
102 int value = chooser.showSaveDialog(parent);
104 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
106 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
107 .getSelectedFile().getParent());
108 file = chooser.getSelectedFile();
116 out = new FileOutputStream(file);
117 if (type == TYPE.SVG)
119 setupSVG(width, height, fileTitle);
121 else if (type == TYPE.EPS)
123 setupEPS(width, height, fileTitle);
125 else if (type == TYPE.PNG)
127 setupPNG(width, height);
130 } catch (Exception ex)
132 System.out.println("Error creating "
133 + type.getName() + " file.");
138 public Graphics getGraphics()
145 public void writeImage()
156 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
157 out.write(svgData.getBytes());
162 ImageIO.write(bi, "png", out);
167 } catch (Exception ex)
169 ex.printStackTrace();
173 void setupEPS(int width, int height, String title)
175 boolean accurateText = true;
177 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
180 // If we need to prompt, and if the GUI is visible then
181 // Prompt for EPS rendering style
182 if (renderStyle.equalsIgnoreCase("Prompt each time")
183 && !(System.getProperty("java.awt.headless") != null && System
184 .getProperty("java.awt.headless").equals("true")))
186 EPSOptions eps = new EPSOptions();
187 renderStyle = eps.getValue();
189 if (renderStyle == null || eps.cancelled)
195 if (renderStyle.equalsIgnoreCase("text"))
197 accurateText = false;
202 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
204 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
205 RenderingHints.VALUE_ANTIALIAS_ON);
207 pg.setAccurateTextMode(accurateText);
210 } catch (Exception ex)
215 void setupPNG(int width, int height)
217 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
218 graphics = bi.getGraphics();
219 Graphics2D ig2 = (Graphics2D) graphics;
220 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
221 RenderingHints.VALUE_ANTIALIAS_ON);
224 void setupSVG(int width, int height, String title)
226 g2 = new SVGGraphics2D(width, height);
230 static JalviewFileChooser getPNGChooser()
232 return new jalview.io.JalviewFileChooser(
233 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
234 { "png" }, new String[]
235 { "Portable network graphics" }, "Portable network graphics");
238 static JalviewFileChooser getEPSChooser()
240 return new jalview.io.JalviewFileChooser(
241 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
242 { "eps" }, new String[]
243 { "Encapsulated Postscript" }, "Encapsulated Postscript");
246 static JalviewFileChooser getSVGChooser()
248 return new jalview.io.JalviewFileChooser(
249 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
250 { "svg" }, new String[]
251 { "Scalable Vector Graphics" }, "Scalable Vector Graphics");