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.
24 import javax.imageio.*;
27 import java.awt.image.*;
29 import org.jibble.epsgraphics.*;
33 public class ImageMaker
35 public static final int EPS = 0;
37 public static final int PNG = 1;
49 public ImageMaker(Component parent, int type, String title, int width,
50 int height, File file, String EPStitle)
56 JalviewFileChooser chooser;
57 chooser = type == EPS ? getEPSChooser() : getPNGChooser();
59 chooser.setFileView(new jalview.io.JalviewFileView());
60 chooser.setDialogTitle(title);
61 chooser.setToolTipText(MessageManager.getString("action.save"));
63 int value = chooser.showSaveDialog(parent);
65 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
67 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
68 .getSelectedFile().getParent());
70 file = chooser.getSelectedFile();
78 out = new FileOutputStream(file);
82 setupEPS(width, height, EPStitle);
86 setupPNG(width, height);
88 } catch (Exception ex)
90 System.out.println("Error creating "
91 + (type == EPS ? "EPS" : "PNG") + " file.");
96 public Graphics getGraphics()
101 void setupPNG(int width, int height)
103 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
104 graphics = bi.getGraphics();
105 Graphics2D ig2 = (Graphics2D) graphics;
106 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
107 RenderingHints.VALUE_ANTIALIAS_ON);
110 public void writeImage()
121 ImageIO.write(bi, "png", out);
125 } catch (Exception ex)
127 ex.printStackTrace();
131 void setupEPS(int width, int height, String title)
133 boolean accurateText = true;
135 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
138 // If we need to prompt, and if the GUI is visible then
139 // Prompt for EPS rendering style
140 if (renderStyle.equalsIgnoreCase("Prompt each time")
141 && !(System.getProperty("java.awt.headless") != null && System
142 .getProperty("java.awt.headless").equals("true")))
144 EPSOptions eps = new EPSOptions();
145 renderStyle = eps.getValue();
147 if (renderStyle == null || eps.cancelled)
153 if (renderStyle.equalsIgnoreCase("text"))
155 accurateText = false;
160 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
161 Graphics2D ig2 = (Graphics2D) pg;
162 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
163 RenderingHints.VALUE_ANTIALIAS_ON);
165 pg.setAccurateTextMode(accurateText);
168 } catch (Exception ex)
173 JalviewFileChooser getPNGChooser()
175 return new jalview.io.JalviewFileChooser(
176 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
177 { "png" }, new String[]
178 { "Portable network graphics" }, "Portable network graphics");
181 JalviewFileChooser getEPSChooser()
183 return new jalview.io.JalviewFileChooser(
184 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
185 { "eps" }, new String[]
186 { "Encapsulated Postscript" }, "Encapsulated Postscript");