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, File file,
116 String fileTitle, boolean useLineart) throws IOException
118 this.type = imageType;
120 out = new FileOutputStream(file);
124 setupSVG(width, height, useLineart);
127 setupEPS(width, height, fileTitle, useLineart);
130 setupPNG(width, height);
136 public Graphics getGraphics()
142 * For SVG or PNG, writes the generated graphics data to the file output
143 * stream. For EPS, flushes the output graphics (which is written to file as
146 public void writeImage()
157 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
158 out.write(svgData.getBytes());
163 ImageIO.write(bi, PNG_EXTENSION, out);
168 } catch (Exception ex)
170 ex.printStackTrace();
175 * Sets up a graphics object for the PNG image to be written on
180 protected void setupPNG(int width, int height)
182 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
183 graphics = bi.getGraphics();
184 Graphics2D ig2 = (Graphics2D) graphics;
185 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
186 RenderingHints.VALUE_ANTIALIAS_ON);
190 * A helper method to configure the SVG output graphics, with choice of Text
191 * or Lineart character rendering
196 * true for Lineart character rendering, false for Text
198 protected void setupSVG(int width, int height, boolean useLineart)
200 SVGGraphics2D g2 = new SVGGraphics2D(width, height);
203 g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
204 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
210 * A helper method that sets up the EPS graphics output with user choice of
211 * Text or Lineart character rendering
217 * true for Lineart character rendering, false for Text
218 * @throws IOException
220 protected void setupEPS(int width, int height, String title,
221 boolean useLineart) throws IOException
223 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
225 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
226 RenderingHints.VALUE_ANTIALIAS_ON);
227 pg.setAccurateTextMode(useLineart);