*/
package jalview.util;
-import java.io.*;
-import javax.imageio.*;
+import jalview.gui.EPSOptions;
+import jalview.io.JalviewFileChooser;
-import java.awt.*;
-import java.awt.image.*;
+import java.awt.Component;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.RenderingHints;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileOutputStream;
-import org.jibble.epsgraphics.*;
-import jalview.gui.*;
-import jalview.io.*;
+import javax.imageio.ImageIO;
+
+import org.jfree.graphics2d.svg.SVGGraphics2D;
+import org.jibble.epsgraphics.EpsGraphics2D;
public class ImageMaker
{
- public static final int EPS = 0;
-
- public static final int PNG = 1;
-
- int type = -1;
+ // public static final int EPS = 0;
+ //
+ // public static final int PNG = 1;
+ //
+ // public static final int SVG = 2;
+ //
+ // int type = -1;
EpsGraphics2D pg;
+ SVGGraphics2D g2;
+
Graphics graphics;
FileOutputStream out;
BufferedImage bi;
- public ImageMaker(Component parent, int type, String title, int width,
- int height, File file, String EPStitle)
+ TYPE type;
+
+ public enum TYPE
+ {
+ EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG(
+ "PNG", MessageManager.getString("label.png_image"),
+ getPNGChooser()), SVG("SVG", "SVG", getSVGChooser());
+
+ private JalviewFileChooser chooser;
+
+ private String name;
+
+ private String label;
+
+ TYPE(String name, String label, JalviewFileChooser chooser)
+ {
+ this.name = name;
+ this.label = label;
+ this.chooser = chooser;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public JalviewFileChooser getChooser()
+ {
+ return chooser;
+ }
+
+ public String getLabel()
+ {
+ return label;
+ }
+
+ }
+
+
+ public ImageMaker(Component parent, TYPE type, String title, int width,
+ int height, File file, String fileTitle)
{
this.type = type;
if (file == null)
{
JalviewFileChooser chooser;
- chooser = type == EPS ? getEPSChooser() : getPNGChooser();
+ // chooser = type == EPS ? getEPSChooser() : getPNGChooser();
+ chooser = type.getChooser();
chooser.setFileView(new jalview.io.JalviewFileView());
chooser.setDialogTitle(title);
try
{
out = new FileOutputStream(file);
-
- if (type == EPS)
+ if (type == TYPE.SVG)
+ {
+ setupSVG(width, height, fileTitle);
+ }
+ else if (type == TYPE.EPS)
{
- setupEPS(width, height, EPStitle);
+ setupEPS(width, height, fileTitle);
}
- else
+ else if (type == TYPE.PNG)
{
setupPNG(width, height);
}
+
} catch (Exception ex)
{
System.out.println("Error creating "
- + (type == EPS ? "EPS" : "PNG") + " file.");
+ + type.getName() + " file.");
}
}
}
return graphics;
}
- void setupPNG(int width, int height)
- {
- bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- graphics = bi.getGraphics();
- Graphics2D ig2 = (Graphics2D) graphics;
- ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- }
+
public void writeImage()
{
pg.flush();
pg.close();
break;
+ case SVG:
+ String svg = ((SVGGraphics2D) getGraphics()).getSVGDocument();
+ out.write(svg.getBytes());
+ out.close();
+ break;
case PNG:
ImageIO.write(bi, "png", out);
out.close();
try
{
pg = new EpsGraphics2D(title, out, 0, 0, width, height);
- Graphics2D ig2 = (Graphics2D) pg;
+ Graphics2D ig2 = pg;
ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
}
- JalviewFileChooser getPNGChooser()
+ void setupPNG(int width, int height)
+ {
+ bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+ graphics = bi.getGraphics();
+ Graphics2D ig2 = (Graphics2D) graphics;
+ ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+ RenderingHints.VALUE_ANTIALIAS_ON);
+ }
+
+ void setupSVG(int width, int height, String title)
+ {
+ g2 = new SVGGraphics2D(width, height);
+ graphics = g2;
+ }
+
+ static JalviewFileChooser getPNGChooser()
{
return new jalview.io.JalviewFileChooser(
jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
{ "Portable network graphics" }, "Portable network graphics");
}
- JalviewFileChooser getEPSChooser()
+ static JalviewFileChooser getEPSChooser()
{
return new jalview.io.JalviewFileChooser(
jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
{ "eps" }, new String[]
{ "Encapsulated Postscript" }, "Encapsulated Postscript");
}
+
+ static JalviewFileChooser getSVGChooser()
+ {
+ return new jalview.io.JalviewFileChooser(
+ jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
+ { "svg" }, new String[]
+ { "Scalable Vector Graphics" }, "Scalable Vector Graphics");
+ }
}