Readline instead of bit by bit
[jalview.git] / src / jalview / util / ImageMaker.java
1 package jalview.util;\r
2 \r
3 import java.awt.event.ActionEvent;\r
4 import java.awt.RenderingHints;\r
5 import javax.imageio.ImageIO;\r
6 import org.jibble.epsgraphics.EpsGraphics2D;\r
7 import jalview.gui.EPSOptions;\r
8 import java.awt.Graphics2D;\r
9 import java.io.*;\r
10 import java.awt.image.BufferedImage;\r
11 import java.awt.Graphics;\r
12 import jalview.io.*;\r
13 import java.awt.*;\r
14 \r
15 \r
16 public class ImageMaker\r
17 {\r
18   public static final int EPS = 0;\r
19   public static final int PNG = 1;\r
20   int type = -1;\r
21 \r
22   EpsGraphics2D pg;\r
23   Graphics graphics;\r
24   FileOutputStream out;\r
25   BufferedImage bi;\r
26 \r
27   public ImageMaker(Component parent, int type, String title,\r
28                            int width, int height, File file, String EPStitle)\r
29   {\r
30     this.type = type;\r
31 \r
32     if (file == null)\r
33     {\r
34       JalviewFileChooser chooser;\r
35       chooser = type == EPS ? getEPSChooser() : getPNGChooser();\r
36 \r
37       chooser.setFileView(new jalview.io.JalviewFileView());\r
38       chooser.setDialogTitle(title);\r
39       chooser.setToolTipText("Save");\r
40 \r
41       int value = chooser.showSaveDialog(parent);\r
42 \r
43       if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)\r
44       {\r
45         jalview.bin.Cache.setProperty("LAST_DIRECTORY",\r
46                                       chooser.getSelectedFile().getParent());\r
47 \r
48         file = chooser.getSelectedFile();\r
49       }\r
50     }\r
51 \r
52     if(file!=null)\r
53     {\r
54       try\r
55       {\r
56         out = new FileOutputStream(file);\r
57 \r
58         if (type == EPS)\r
59           setupEPS(width, height, EPStitle);\r
60         else\r
61           setupPNG(width, height);\r
62       }\r
63       catch (Exception ex)\r
64       {\r
65         System.out.println("Error creating " + (type == EPS ? "EPS" : "PNG") +\r
66                            " file.");\r
67       }\r
68     }\r
69   }\r
70 \r
71   public Graphics getGraphics()\r
72   {\r
73     return graphics;\r
74   }\r
75 \r
76 \r
77   void setupPNG(int width, int height)\r
78   {\r
79         bi = new BufferedImage(width, height,\r
80                 BufferedImage.TYPE_INT_RGB);\r
81         graphics  = bi.getGraphics();\r
82         Graphics2D ig2 = (Graphics2D) graphics;\r
83         ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
84                                  RenderingHints.VALUE_ANTIALIAS_ON);\r
85   }\r
86 \r
87   public void writeImage()\r
88   {\r
89     try{\r
90       switch(type)\r
91       {\r
92         case EPS:\r
93           pg.flush();\r
94           pg.close();\r
95           break;\r
96         case PNG:\r
97           ImageIO.write(bi, "png", out);\r
98           out.close();\r
99           break;\r
100       }\r
101     }\r
102     catch (Exception ex)\r
103     {\r
104         ex.printStackTrace();\r
105     }\r
106   }\r
107 \r
108   void setupEPS(int width, int height, String title)\r
109   {\r
110     boolean accurateText = true;\r
111 \r
112     String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",\r
113         "Prompt each time");\r
114 \r
115     // If we need to prompt, and if the GUI is visible then\r
116     // Prompt for EPS rendering style\r
117     if (renderStyle.equalsIgnoreCase("Prompt each time")\r
118         && !\r
119         (System.getProperty("java.awt.headless") != null\r
120          && System.getProperty("java.awt.headless").equals("true")))\r
121     {\r
122       EPSOptions eps = new EPSOptions();\r
123       renderStyle = eps.getValue();\r
124 \r
125       if (renderStyle == null || eps.cancelled)\r
126         return;\r
127     }\r
128 \r
129     if (renderStyle.equalsIgnoreCase("text"))\r
130     {\r
131       accurateText = false;\r
132     }\r
133 \r
134     try   {\r
135       pg = new EpsGraphics2D(title, out, 0, 0, width,\r
136                                            height);\r
137       Graphics2D ig2 = (Graphics2D) pg;\r
138       ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
139                            RenderingHints.VALUE_ANTIALIAS_ON);\r
140 \r
141       pg.setAccurateTextMode(accurateText);\r
142 \r
143       graphics = pg;\r
144     }\r
145     catch (Exception ex) {  }\r
146   }\r
147 \r
148 \r
149   JalviewFileChooser getPNGChooser()\r
150   {\r
151     return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(\r
152         "LAST_DIRECTORY"), new String[]\r
153                                              {"png"},\r
154                                              new String[]\r
155                                              {"Portable network graphics"},\r
156                                              "Portable network graphics");\r
157   }\r
158 \r
159   JalviewFileChooser getEPSChooser()\r
160   {\r
161     return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(\r
162         "LAST_DIRECTORY"), new String[]\r
163                                              {"eps"},\r
164                                              new String[]\r
165                                              {"Encapsulated Postscript"},\r
166                                              "Encapsulated Postscript");\r
167   }\r
168 }\r