1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/*
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. <br>
* <br>
*
* 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.
*
*
*/
|