KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > util > export > PNGTools


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.util.export;
8
9 import java.awt.Component JavaDoc;
10 import java.awt.image.BufferedImage JavaDoc;
11 import java.awt.image.RenderedImage JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 import javax.swing.JFileChooser JavaDoc;
16 import javax.swing.filechooser.FileFilter JavaDoc;
17
18 import org.apache.log4j.Logger;
19 import org.ejtools.util.Platform;
20
21
22 /**
23  * Description of the Class
24  *
25  * @author Laurent Etiemble
26  * @version $Revision: 1.2 $
27  */

28 public class PNGTools
29 {
30    /** Description of the Field */
31    private static FileFilter JavaDoc PNG_FILE_FILTER =
32       new FileFilter JavaDoc()
33       {
34          public boolean accept(File JavaDoc file)
35          {
36             return file.getName().endsWith(".png");
37          }
38
39
40          public String JavaDoc getDescription()
41          {
42             return "PNG image (*.png)";
43          }
44       };
45    /** Description of the Field */
46    private static Logger logger = Logger.getLogger(PNGTools.class);
47
48
49    /**Constructor for the PNGCreator object */
50    private PNGTools()
51    {
52       super();
53    }
54
55
56    /**
57     * Description of the Method
58     *
59     * @param image Description of the Parameter
60     * @param output Description of the Parameter
61     */

62    public static void exportAsPNG(RenderedImage JavaDoc image, File JavaDoc output)
63    {
64       if (Platform.isJavaVersionCompatible(Platform.JAVA_1_4))
65       {
66          try
67          {
68             // Qualified name is mandatory
69
javax.imageio.ImageIO.write(image, "PNG", output);
70          }
71          catch (IOException JavaDoc ioe)
72          {
73             logger.error("Can't export image as PNG", ioe);
74          }
75       }
76    }
77
78
79    /**
80     * Description of the Method
81     *
82     * @param component Description of the Parameter
83     * @param output Description of the Parameter
84     */

85    public static void exportAsPNG(Component JavaDoc component, File JavaDoc output)
86    {
87       if (Platform.isJavaVersionCompatible(Platform.JAVA_1_4))
88       {
89          try
90          {
91             BufferedImage JavaDoc image = paintAsPNG(component);
92             // Qualified name is mandatory
93
javax.imageio.ImageIO.write(image, "PNG", output);
94          }
95          catch (IOException JavaDoc ioe)
96          {
97             logger.error("Can't export image as PNG", ioe);
98          }
99       }
100    }
101
102
103    /**
104     * Description of the Method
105     *
106     * @param component Description of the Parameter
107     * @return Description of the Return Value
108     */

109    public static BufferedImage JavaDoc paintAsPNG(Component JavaDoc component)
110    {
111       BufferedImage JavaDoc image = null;
112
113       image = new BufferedImage JavaDoc(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_ARGB);
114       component.paint(image.getGraphics());
115
116       return image;
117    }
118
119
120    /**
121     * Description of the Method
122     *
123     * @return Description of the Return Value
124     */

125    public static File JavaDoc selectPNGFile()
126    {
127       // Fix for JFileChooser/SecurityManager bug (#4264750)
128
SecurityManager JavaDoc s = System.getSecurityManager();
129       System.setSecurityManager(null);
130
131       // Choose file
132
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc(System.getProperty("user.dir"));
133       chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
134       chooser.setFileFilter(PNG_FILE_FILTER);
135       int returnVal = chooser.showSaveDialog(null);
136       System.setSecurityManager(s);
137       if (returnVal != JFileChooser.APPROVE_OPTION)
138       {
139          return null;
140       }
141       return chooser.getSelectedFile();
142    }
143 }
144
Popular Tags