/* Wotonomy: OpenStep design patterns for pure Java applications. Copyright (C) 2000 Michael Powers This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see http://www.gnu.org */ package net.wotonomy.ui.swing.util; import java.awt.Component; import java.awt.Image; import java.awt.Window; import java.io.File; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.OutputStream; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JWindow; /** * WindowGrabber is a collection of static utility methods for taking screen * shots of lightweight containers. All components that are not native peers * will be drawn to a bitmap that will be run-length compressed (LZW encoding, * GIF format) and written to the specified file or output stream.
*
* * Any Swing/JFC or IFC window is a good candidate for use with these methods. * The component is not expected to contain more than 256 colors (the maximum * that GIF allows). While there is a color depth limitation, the compression is * lossless, with no blurs or bleeding color. * * @author michael@mpowers.net * @version $Revision: 904 $ $Date: 2006-02-18 18:19:05 -0500 (Sat, 18 Feb 2006) * $ */ public class WindowGrabber { /** * Captures the screen contents of the specified component, and write it to a * file with the specified name. * * @param aComponent a lightweight component or container. * @param aFileName the name of the file to write, optionally preceded by path. * @return True if the file was successfully written, false if there was an * error. Errors are written to the standard error stream. */ static public boolean grab(Component aComponent, String aFileName) { OutputStream output = null; try { output = new FileOutputStream(aFileName); } catch (Exception exc) { System.err.println(exc); return false; } return grab(aComponent, output); } /** * Captures the screen contents of the specified component, and write it to a * file with the specified name. * * @param aComponent a lightweight component or container. * @param anOutputStream an output stream to which the image will be written. * @return True if the image was successfully written, false if there was an * error. Errors are written to the standard error stream. */ static public boolean grab(Component aComponent, OutputStream anOutputStream) { Image img = aComponent.createImage(aComponent.getSize().width, aComponent.getSize().height); aComponent.paintAll(img.getGraphics()); try { GIFEncoder encoder = new GIFEncoder(img); encoder.write(anOutputStream); anOutputStream.flush(); } catch (Exception exc) { System.err.println(exc); return false; } return true; } protected static void processFilenames(String path, String[] filenames) { ClassLoader loader = new ClassGrabber(); File f; for (int i = 0; i < filenames.length; i++) { try { f = new File(path + filenames[i]); if (f.isDirectory()) { processFilenames(path + filenames[i] + "/", f.list(new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".class"); } })); } else { System.out.println("Loading " + filenames[i] + ": "); Class c = loader.loadClass(path + filenames[i]); System.out.println(c); if (JWindow.class.isAssignableFrom(c) || JDialog.class.isAssignableFrom(c) || JFrame.class.isAssignableFrom(c)) { try { Window w = (Window) c.newInstance(); if (w.getBounds().width * w.getBounds().height == 0) { // if size not specified or set, pack // it w.pack(); } String gifName = // replace .class with .gif filenames[i].substring(0, filenames[i].length() - 5) + "gif"; w.addNotify(); w.repaint(); grab(w, gifName); System.out.println("wrote: " + gifName); } catch (Exception exc) { System.err.println("WindowGrab failed for " + filenames[i] + ": "); exc.printStackTrace(); } } else { System.out.println("not a JWindow, JDialog, or JFrame; ignored."); } } } catch (Exception exc) { System.err.println(filenames[i] + ": " + exc); } } } /** * Captures images of any Swing window component classes specified as * parameters. If created, image file will have the same name as the * corresponding class file, but with a ".gif" extension. * * @param argv a list of filenames or directory names. */ public static void main(String[] argv) { processFilenames("", argv); System.exit(0); } } /* * $Log$ Revision 1.2 2006/02/18 23:19:05 cgruber Update imports and maven * dependencies. * * Revision 1.1 2006/02/16 13:22:22 cgruber Check in all sources in * eclipse-friendly maven-enabled packages. * * Revision 1.1.1.1 2000/12/21 15:51:49 mpowers Contributing wotonomy. * * Revision 1.2 2000/12/20 16:25:46 michael Added log to all files. * * */