-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathThumbnailIcon.java
More file actions
343 lines (316 loc) · 10.6 KB
/
ThumbnailIcon.java
File metadata and controls
343 lines (316 loc) · 10.6 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* @(#)ThumbnailIcon.java 1.0 04/12/12
*/
package darrylbu.icon;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.net.URL;
/**
* An <CODE>Icon</CODE> that maintains a defined size independent of the size of the
* image it contains. If needed, the size of its image is reduced to fit; otherwise
* the image is displayed at its natural size, padded and centered horizontally and/or
* vertically. The size of an individual <code>ThumbnailIcon</code>, or the default
* size of all <CODE>ThumbnailIcon</CODE>s, can be changed by the static method provided.
* <P>
* Swing components that use icons are not notified when the icon's size is changed.
* In case the size of a <CODE>ThumbnailIcon</CODE> displayed in an already realized
* component is changed, either by setting its size or the default size, it is the
* caller's responsibility to revalidate the component hierarchy affected by the change.
* <P>
* The class is a drop-in replacement for <CODE>ImageIcon</CODE>, except that
* the no-argument constructor is not supported.
* <P>
* <CODE>ThumbnailIcon</CODE>, as its name implies, is useful for displaying
* multiple images of different sizes, reduced or padded as necessary to fit the
* same dimensions.
* <P>
* If not set, the default size of <CODE>ThumbnailIcon</CODE> is 160 X 120 pixels.
*
* @version 1.0 04/12/12
* @author Darryl
*/
public class ThumbnailIcon extends ShrinkIcon {
/**
* The constant for setting the width and/or height to use the default
* for the class.
*/
public static final int DEFAULT = -1;
/**
* The constant for setting the width, height, default width or default height
* to be computed from the other dimension in a way that maintains the aspect
* ratio of the contained image.
* <P>
* If both the width and height evaluate to <CODE>COMPUTED</CODE>, the image
* will be displayed at its natural size, essentially mimicking the behavior
* of <CODE>ImageIcon</CODE>.
*/
public static final int COMPUTED = 0;
private static int defaultWidth = 160;
private static int defaultHeight = 120;
private int thumbWidth = DEFAULT;
private int thumbHeight = DEFAULT;
/**
* Creates a <CODE>ThumbnailIcon</CODE> from an array of bytes.
*
* @param imageData an array of pixels in an image format supported by
* the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
*
* @see ImageIcon#ImageIcon(byte[])
*/
public ThumbnailIcon(byte[] imageData) {
super(imageData);
}
/**
* Creates a <CODE>ThumbnailIcon</CODE> from an array of bytes.
*
* @param imageData an array of pixels in an image format supported by
* the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
* @param description a brief textual description of the image
*
* @see ImageIcon#ImageIcon(byte[], java.lang.String)
*/
public ThumbnailIcon(byte[] imageData, String description) {
super(imageData, description);
}
/**
* Creates a <CODE>ThumbnailIcon</CODE> from the image.
*
* @param image the image
*
* @see ImageIcon#ImageIcon(java.awt.Image)
*/
public ThumbnailIcon(Image image) {
super(image);
}
/**
* Creates a <CODE>ThumbnailIcon</CODE> from the image.
*
* @param image the image
* @param description a brief textual description of the image
*
* @see ImageIcon#ImageIcon(java.awt.Image, java.lang.String)
*/
public ThumbnailIcon(Image image, String description) {
super(image, description);
}
/**
* Creates a <CODE>ThumbnailIcon</CODE> from the specified file.
*
* @param filename a String specifying a filename or path
*
* @see ImageIcon#ImageIcon(java.lang.String)
*/
public ThumbnailIcon(String filename) {
super(filename);
}
/**
* Creates a <CODE>ThumbnailIcon</CODE> from the specified file.
*
* @param filename a String specifying a filename or path
* @param description a brief textual description of the image
*
* @see ImageIcon#ImageIcon(java.lang.String, java.lang.String)
*/
public ThumbnailIcon(String filename, String description) {
super(filename, description);
}
/**
* Creates a <CODE>ThumbnailIcon</CODE> from the specified URL.
*
* @param location the URL for the image
*
* @see ImageIcon#ImageIcon(java.net.URL)
*/
public ThumbnailIcon(URL location) {
super(location);
}
/**
* Creates a <CODE>ThumbnailIcon</CODE> from the specified URL.
*
* @param location the URL for the image
* @param description a brief textual description of the image
*
* @see ImageIcon#ImageIcon(java.net.URL, java.lang.String)
*/
public ThumbnailIcon(URL location, String description) {
super(location, description);
}
/**
* Sets the width of an individual <CODE>ThumbnailIcon</CODE>.
* <P>
* Set the width to <CODE>DEFAULT</CODE> to use the default width for the class,
* or set it to <CODE>COMPUTED</CODE> for the width to be computed form the height,
* maintaining the aspect ratio of the contained image.
* <P>
* If both the width and height evaluate to <CODE>COMPUTED</CODE>, the image
* will be displayed at its natural size, essentially mimicking the behavior
* of <CODE>ImageIcon</CODE>.
* <P>
* Setting a value less than <CODE>DEFAULT</CODE> is not permitted.
*
* @param width The width to set
*/
public void setThumbWidth(int width) {
if (width < DEFAULT) {
throw new IllegalArgumentException("Width cannot be less than ThumbnailIcon.DEFAULT");
}
thumbWidth = width;
}
/**
* Returns the width that has been set, or <CODE>DEFAULT</CODE>. Note that this
* is not the same value as that returned by <CODE>getIconWidth()</CODE>.
*
* @return the set width, or <CODE>DEFAULT</CODE> if none has been set.
*/
public int getThumbWidth() {
return thumbWidth;
}
/**
* Sets the height of an individual <CODE>ThumbnailIcon</CODE>.
* <P>
* Set the height to <CODE>DEFAULT</CODE> to use the default height for the class,
* or set it to <CODE>COMPUTED</CODE> for the height to be computed form the width,
* maintaining the aspect ratio of the contained image.
* <P>
* If both the width and height evaluate to <CODE>COMPUTED</CODE>, the image
* will be displayed at its natural size, essentially mimicking the behavior
* of <CODE>ImageIcon</CODE>.
* <P>
* Setting a value less than <CODE>DEFAULT</CODE> is not permitted.
*
* @param height The height to set
*/
public void setThumbHeight(int height) {
if (height < DEFAULT) {
throw new IllegalArgumentException("Height cannot be less than ThumbnailIcon.DEFAULT");
}
thumbHeight = height;
}
/**
* Returns the height that has been set, or <CODE>DEFAULT</CODE>. Note that this
* is not the same value as that returned by <CODE>getIconHeight()</CODE>.
*
* @return the set height, or <CODE>DEFAULT</CODE> if none has been set.
*/
public int getThumbHeight() {
return thumbHeight;
}
/**
* Sets the default size of all <CODE>ThumbnailIcon</CODE>s.
* <P>
* If either the width or height is zero it will be computed from the other
* dimension, according to the aspect ratio of each <CODE>ThumbnaiIcon</CODE>'s
* contained image. Setting the default width to zero may be useful for a horizontal
* layout of components that display a <CODE>ThumbnailIcon</CODE>; correspondingly,
* a default height of zero can be useful in a vertical layout.
* <P>
* If both width and height are zero, a <CODE>ThumbnailIcon</CODE> that respects the
* default width and height will be sized to its contained image, essentially
* mimicking the behavior of <CODE>ImageIcon</CODE>.
*
* @param width The default width to set.
* @param height The default height to set.
*/
public static void setDefaultSize(int width, int height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Default width and/or height cannot be negative");
}
defaultWidth = width;
defaultHeight = height;
}
/**
* Gets the default width of all <CODE>ThumbnailIcon</CODE>s. A return value
* of zero indicates that the default width will be computed from the default
* height so as to maintain the aspect ratio of the contained image.
*
* @return The default width for all <CODE>ThumbnailIcon</CODE>s
*/
public static int getDefaultWidth() {
return defaultWidth;
}
/**
* Gets the default height of all <CODE>ThumbnailIcon</CODE>s. A return value
* of zero indicates that the default height will be computed from the default
* width so as to maintain the aspect ratio of the contained image.
*
* @return The default width for all <CODE>ThumbnailIcon</CODE>s
*/
public static int getDefaultHeight() {
return defaultHeight;
}
/**
* Gets the width of the icon.
*
* @return The width in pixels of this icon
*/
@Override
public int getIconWidth() {
if (thumbWidth > 0) {
return thumbWidth;
}
Image image = getImage();
int w = image.getWidth(component);
int h = image.getHeight(component);
if (thumbWidth < 0) {
if (defaultWidth > 0) {
return defaultWidth;
}
if (defaultHeight > 0) {
return (defaultHeight * w) / h;
}
if (thumbHeight <= 0) {
return w;
}
}
return (getIconHeight() * w) / h;
}
/**
* Gets the height of the icon.
*
* @return The height in pixels of this icon
*/
@Override
public int getIconHeight() {
if (thumbHeight > 0) {
return thumbHeight;
}
Image image = getImage();
int w = image.getWidth(component);
int h = image.getHeight(component);
if (thumbHeight < 0) {
if (defaultHeight > 0) {
return defaultHeight;
}
if (defaultWidth > 0) {
return (defaultWidth * h) / w;
}
if (thumbWidth <= 0) {
return h;
}
}
return (getIconWidth() * h) / w;
}
/**
* Convenience method to obtain the icon's image at the size at which it is displayed.
* Note that a thumbnail obtained from an animated image will not itself be animated.
*
* @return The thumbnail image.
*/
public BufferedImage getThumbnail() {
Image image = getImage();
int w = image.getWidth(component);
int h = image.getHeight(component);
int tw = getIconWidth();
int th = getIconHeight();
if (w * th < h * tw) {
tw = (th * w) / h;
} else {
th = (tw * h) / w;
}
tw = tw > w ? w : tw;
th = th > h ? h : th;
BufferedImage retVal = new BufferedImage(tw, th, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(getImage(), 0, 0, tw, th, component);
return retVal;
}
}