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.gui.EPSOptions;
24 import jalview.gui.SVGOptions;
25 import jalview.io.JalviewFileChooser;
27 import java.awt.Component;
28 import java.awt.Graphics;
29 import java.awt.Graphics2D;
30 import java.awt.RenderingHints;
31 import java.awt.image.BufferedImage;
33 import java.io.FileOutputStream;
35 import javax.imageio.ImageIO;
37 import org.jfree.graphics2d.svg.SVGGraphics2D;
38 import org.jfree.graphics2d.svg.SVGHints;
39 import org.jibble.epsgraphics.EpsGraphics2D;
41 public class ImageMaker
57 EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG(
58 "PNG", MessageManager.getString("label.png_image"),
59 getPNGChooser()), SVG("SVG", "SVG", getSVGChooser());
61 private JalviewFileChooser chooser;
67 TYPE(String name, String label, JalviewFileChooser chooser)
71 this.chooser = chooser;
74 public String getName()
79 public JalviewFileChooser getChooser()
84 public String getLabel()
92 public ImageMaker(Component parent, TYPE type, String title, int width,
93 int height, File file, String fileTitle)
99 JalviewFileChooser chooser;
100 chooser = type.getChooser();
101 chooser.setFileView(new jalview.io.JalviewFileView());
102 chooser.setDialogTitle(title);
103 chooser.setToolTipText(MessageManager.getString("action.save"));
104 int value = chooser.showSaveDialog(parent);
106 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
108 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
109 .getSelectedFile().getParent());
110 file = chooser.getSelectedFile();
118 out = new FileOutputStream(file);
119 if (type == TYPE.SVG)
121 setupSVG(width, height, fileTitle);
123 else if (type == TYPE.EPS)
125 setupEPS(width, height, fileTitle);
127 else if (type == TYPE.PNG)
129 setupPNG(width, height);
132 } catch (Exception ex)
134 System.out.println("Error creating "
135 + type.getName() + " file.");
140 public Graphics getGraphics()
147 public void writeImage()
158 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
159 out.write(svgData.getBytes());
164 ImageIO.write(bi, "png", out);
169 } catch (Exception ex)
171 ex.printStackTrace();
175 void setupEPS(int width, int height, String title)
177 boolean accurateText = true;
179 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
182 // If we need to prompt, and if the GUI is visible then
183 // Prompt for EPS rendering style
184 if (renderStyle.equalsIgnoreCase("Prompt each time")
185 && !(System.getProperty("java.awt.headless") != null && System
186 .getProperty("java.awt.headless").equals("true")))
188 EPSOptions eps = new EPSOptions();
189 renderStyle = eps.getValue();
191 if (renderStyle == null || eps.cancelled)
197 if (renderStyle.equalsIgnoreCase("text"))
199 accurateText = false;
204 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
206 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
207 RenderingHints.VALUE_ANTIALIAS_ON);
209 pg.setAccurateTextMode(accurateText);
212 } catch (Exception ex)
217 void setupPNG(int width, int height)
219 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
220 graphics = bi.getGraphics();
221 Graphics2D ig2 = (Graphics2D) graphics;
222 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
223 RenderingHints.VALUE_ANTIALIAS_ON);
227 void setupSVG(int width, int height, String title)
230 g2 = new SVGGraphics2D(width, height);
233 String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
236 // If we need to prompt, and if the GUI is visible then
237 // Prompt for EPS rendering style
238 if (renderStyle.equalsIgnoreCase("Prompt each time")
239 && !(System.getProperty("java.awt.headless") != null && System
240 .getProperty("java.awt.headless").equals("true")))
242 SVGOptions svgOption = new SVGOptions();
243 renderStyle = svgOption.getValue();
245 if (renderStyle == null || svgOption.cancelled)
251 if (renderStyle.equalsIgnoreCase("lineart"))
253 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
254 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
260 static JalviewFileChooser getPNGChooser()
262 return new jalview.io.JalviewFileChooser(
263 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
264 { "png" }, new String[]
265 { "Portable network graphics" }, "Portable network graphics");
268 static JalviewFileChooser getEPSChooser()
270 return new jalview.io.JalviewFileChooser(
271 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
272 { "eps" }, new String[]
273 { "Encapsulated Postscript" }, "Encapsulated Postscript");
276 static JalviewFileChooser getSVGChooser()
278 return new jalview.io.JalviewFileChooser(
279 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
280 { "svg" }, new String[]
281 { "Scalable Vector Graphics" }, "Scalable Vector Graphics");