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
41 // public static final int EPS = 0;
43 // public static final int PNG = 1;
45 // public static final int SVG = 2;
63 EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG(
64 "PNG", MessageManager.getString("label.png_image"),
65 getPNGChooser()), SVG("SVG", "SVG", getSVGChooser());
67 private JalviewFileChooser chooser;
73 TYPE(String name, String label, JalviewFileChooser chooser)
77 this.chooser = chooser;
80 public String getName()
85 public JalviewFileChooser getChooser()
90 public String getLabel()
98 public ImageMaker(Component parent, TYPE type, String title, int width,
99 int height, File file, String fileTitle)
105 JalviewFileChooser chooser;
106 // chooser = type == EPS ? getEPSChooser() : getPNGChooser();
107 chooser = type.getChooser();
109 chooser.setFileView(new jalview.io.JalviewFileView());
110 chooser.setDialogTitle(title);
111 chooser.setToolTipText(MessageManager.getString("action.save"));
113 int value = chooser.showSaveDialog(parent);
115 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
117 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
118 .getSelectedFile().getParent());
120 file = chooser.getSelectedFile();
128 out = new FileOutputStream(file);
129 if (type == TYPE.SVG)
131 setupSVG(width, height, fileTitle);
133 else if (type == TYPE.EPS)
135 setupEPS(width, height, fileTitle);
137 else if (type == TYPE.PNG)
139 setupPNG(width, height);
142 } catch (Exception ex)
144 System.out.println("Error creating "
145 + type.getName() + " file.");
150 public Graphics getGraphics()
157 public void writeImage()
168 String svg = ((SVGGraphics2D) getGraphics()).getSVGDocument();
169 out.write(svg.getBytes());
173 ImageIO.write(bi, "png", out);
177 } catch (Exception ex)
179 ex.printStackTrace();
183 void setupEPS(int width, int height, String title)
185 boolean accurateText = true;
187 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
190 // If we need to prompt, and if the GUI is visible then
191 // Prompt for EPS rendering style
192 if (renderStyle.equalsIgnoreCase("Prompt each time")
193 && !(System.getProperty("java.awt.headless") != null && System
194 .getProperty("java.awt.headless").equals("true")))
196 EPSOptions eps = new EPSOptions();
197 renderStyle = eps.getValue();
199 if (renderStyle == null || eps.cancelled)
205 if (renderStyle.equalsIgnoreCase("text"))
207 accurateText = false;
212 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
214 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
215 RenderingHints.VALUE_ANTIALIAS_ON);
217 pg.setAccurateTextMode(accurateText);
220 } catch (Exception ex)
225 void setupPNG(int width, int height)
227 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
228 graphics = bi.getGraphics();
229 Graphics2D ig2 = (Graphics2D) graphics;
230 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
231 RenderingHints.VALUE_ANTIALIAS_ON);
234 void setupSVG(int width, int height, String title)
236 g2 = new SVGGraphics2D(width, height);
240 static JalviewFileChooser getPNGChooser()
242 return new jalview.io.JalviewFileChooser(
243 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
244 { "png" }, new String[]
245 { "Portable network graphics" }, "Portable network graphics");
248 static JalviewFileChooser getEPSChooser()
250 return new jalview.io.JalviewFileChooser(
251 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
252 { "eps" }, new String[]
253 { "Encapsulated Postscript" }, "Encapsulated Postscript");
256 static JalviewFileChooser getSVGChooser()
258 return new jalview.io.JalviewFileChooser(
259 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
260 { "svg" }, new String[]
261 { "Scalable Vector Graphics" }, "Scalable Vector Graphics");