1 7 package org.ejtools.util.export; 8 9 import java.awt.Component ; 10 import java.awt.image.BufferedImage ; 11 import java.awt.image.RenderedImage ; 12 import java.io.File ; 13 import java.io.IOException ; 14 15 import javax.swing.JFileChooser ; 16 import javax.swing.filechooser.FileFilter ; 17 18 import org.apache.log4j.Logger; 19 import org.ejtools.util.Platform; 20 21 22 28 public class PNGTools 29 { 30 31 private static FileFilter PNG_FILE_FILTER = 32 new FileFilter () 33 { 34 public boolean accept(File file) 35 { 36 return file.getName().endsWith(".png"); 37 } 38 39 40 public String getDescription() 41 { 42 return "PNG image (*.png)"; 43 } 44 }; 45 46 private static Logger logger = Logger.getLogger(PNGTools.class); 47 48 49 50 private PNGTools() 51 { 52 super(); 53 } 54 55 56 62 public static void exportAsPNG(RenderedImage image, File output) 63 { 64 if (Platform.isJavaVersionCompatible(Platform.JAVA_1_4)) 65 { 66 try 67 { 68 javax.imageio.ImageIO.write(image, "PNG", output); 70 } 71 catch (IOException ioe) 72 { 73 logger.error("Can't export image as PNG", ioe); 74 } 75 } 76 } 77 78 79 85 public static void exportAsPNG(Component component, File output) 86 { 87 if (Platform.isJavaVersionCompatible(Platform.JAVA_1_4)) 88 { 89 try 90 { 91 BufferedImage image = paintAsPNG(component); 92 javax.imageio.ImageIO.write(image, "PNG", output); 94 } 95 catch (IOException ioe) 96 { 97 logger.error("Can't export image as PNG", ioe); 98 } 99 } 100 } 101 102 103 109 public static BufferedImage paintAsPNG(Component component) 110 { 111 BufferedImage image = null; 112 113 image = new BufferedImage (component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_ARGB); 114 component.paint(image.getGraphics()); 115 116 return image; 117 } 118 119 120 125 public static File selectPNGFile() 126 { 127 SecurityManager s = System.getSecurityManager(); 129 System.setSecurityManager(null); 130 131 JFileChooser chooser = new JFileChooser (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 |