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.io.JalviewFileChooser;
25 import java.awt.Graphics;
26 import java.awt.Graphics2D;
27 import java.awt.RenderingHints;
28 import java.awt.image.BufferedImage;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
33 import javax.imageio.ImageIO;
35 import org.jfree.graphics2d.svg.SVGGraphics2D;
36 import org.jfree.graphics2d.svg.SVGHints;
37 import org.jibble.epsgraphics.EpsGraphics2D;
39 public class ImageMaker
41 public static final String SVG_DESCRIPTION = "Scalable Vector Graphics";
43 public static final String SVG_EXTENSION = "svg";
45 public static final String EPS_DESCRIPTION = "Encapsulated Postscript";
47 public static final String EPS_EXTENSION = "eps";
49 public static final String PNG_EXTENSION = "png";
51 public static final String PNG_DESCRIPTION = "Portable network graphics";
65 EPS("EPS", MessageManager.getString("label.eps_file"), EPS_EXTENSION,
67 PNG("PNG", MessageManager.getString("label.png_image"), PNG_EXTENSION,
69 SVG("SVG", "SVG", SVG_EXTENSION, SVG_DESCRIPTION);
71 public final String name;
73 public final String label;
75 public final String extension;
77 public final String description;
79 TYPE(String name, String label, String ext, String desc)
84 this.description = desc;
87 public String getName()
92 public JalviewFileChooser getFileChooser()
94 return new JalviewFileChooser(extension, description);
97 public String getLabel()
105 * Constructor configures the graphics context ready for writing to
113 * @throws IOException
115 public ImageMaker(TYPE imageType, int width, int height,
116 File file, String fileTitle, boolean useLineart)
119 this.type = imageType;
121 out = new FileOutputStream(file);
125 setupSVG(width, height, useLineart);
128 setupEPS(width, height, fileTitle, useLineart);
131 setupPNG(width, height);
137 public Graphics getGraphics()
143 * For SVG or PNG, writes the generated graphics data to the file output
144 * stream. For EPS, flushes the output graphics (which is written to file as
147 public void writeImage()
158 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
159 out.write(svgData.getBytes());
164 ImageIO.write(bi, PNG_EXTENSION, out);
169 } catch (Exception ex)
171 ex.printStackTrace();
176 * Sets up a graphics object for the PNG image to be written on
181 protected void setupPNG(int width, int height)
183 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
184 graphics = bi.getGraphics();
185 Graphics2D ig2 = (Graphics2D) graphics;
186 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
187 RenderingHints.VALUE_ANTIALIAS_ON);
191 * A helper method to configure the SVG output graphics, with choice of Text
192 * or Lineart character rendering
197 * true for Lineart character rendering, false for Text
199 protected void setupSVG(int width, int height, boolean useLineart)
201 SVGGraphics2D g2 = new SVGGraphics2D(width, height);
204 g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
205 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
211 * A helper method that sets up the EPS graphics output with user choice of
212 * Text or Lineart character rendering
218 * true for Lineart character rendering, false for Text
219 * @throws IOException
221 protected void setupEPS(int width, int height, String title,
222 boolean useLineart) throws IOException
224 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
226 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
227 RenderingHints.VALUE_ANTIALIAS_ON);
228 pg.setAccurateTextMode(useLineart);