Skip to content

Commit 1349ce3

Browse files
authored
Add files via upload
1 parent 2dc839c commit 1349ce3

File tree

1 file changed

+303
-0
lines changed

1 file changed

+303
-0
lines changed

source/ShrinkIcon.java

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
/**
2+
* @(#)ShrinkIcon.java 1.0 04/05/12
3+
*/
4+
//package darrylbu.icon;
5+
6+
import java.awt.Component;
7+
import java.awt.Container;
8+
import java.awt.Graphics;
9+
import java.awt.Image;
10+
import java.awt.Insets;
11+
import java.awt.image.ImageObserver;
12+
import java.net.URL;
13+
import javax.swing.ImageIcon;
14+
15+
/**
16+
* An <CODE>Icon</CODE> that when necessary reduces the size of its image to
17+
* fit within the component area, excluding any border or insets, optionally
18+
* maintaining the image's aspect ratio by padding and centering the scaled
19+
* image horizontally or vertically. When the component is larger than the image,
20+
* the image will be drawn at its natural size and padded and centered
21+
* horizontally and/or vertically.
22+
* <P>
23+
* The class is a drop-in replacement for <CODE>ImageIcon</CODE>, except that
24+
* the no-argument constructor is not supported.
25+
* <P>
26+
* As the size of this icon is determined by the size of the component in
27+
* which it is displayed, <CODE>ShrinkIcon</CODE> must only be used in
28+
* conjunction with a component and layout that does not depend on the size
29+
* of the component's Icon.
30+
*
31+
* @version 1.0 04/05/12
32+
* @author Darryl
33+
*/
34+
public class ShrinkIcon extends StretchIcon {
35+
36+
/**
37+
* Creates a <CODE>ShrinkIcon</CODE> from an array of bytes.
38+
*
39+
* @param imageData an array of pixels in an image format supported by
40+
* the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
41+
*
42+
* @see ImageIcon#ImageIcon(byte[])
43+
*/
44+
public ShrinkIcon(byte[] imageData) {
45+
super(imageData);
46+
}
47+
48+
/**
49+
* Creates a <CODE>ShrinkIcon</CODE> from an array of bytes with the specified behavior.
50+
*
51+
* @param imageData an array of pixels in an image format supported by
52+
* the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
53+
* @param proportionate <code>true</code> to retain the image's aspect ratio,
54+
* <code>false</code> to allow distortion of the image to fit the
55+
* component.
56+
*
57+
* @see ImageIcon#ImageIcon(byte[])
58+
*/
59+
public ShrinkIcon(byte[] imageData, boolean proportionate) {
60+
super(imageData, proportionate);
61+
}
62+
63+
/**
64+
* Creates a <CODE>ShrinkIcon</CODE> from an array of bytes.
65+
*
66+
* @param imageData an array of pixels in an image format supported by
67+
* the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
68+
* @param description a brief textual description of the image
69+
*
70+
* @see ImageIcon#ImageIcon(byte[], java.lang.String)
71+
*/
72+
public ShrinkIcon(byte[] imageData, String description) {
73+
super(imageData, description);
74+
}
75+
76+
/**
77+
* Creates a <CODE>ShrinkIcon</CODE> from an array of bytes with the specified behavior.
78+
*
79+
* @see ImageIcon#ImageIcon(byte[])
80+
* @param imageData an array of pixels in an image format supported by
81+
* the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
82+
* @param description a brief textual description of the image
83+
* @param proportionate <code>true</code> to retain the image's aspect ratio,
84+
* <code>false</code> to allow distortion of the image to fit the
85+
* component.
86+
*
87+
* @see ImageIcon#ImageIcon(byte[], java.lang.String)
88+
*/
89+
public ShrinkIcon(byte[] imageData, String description, boolean proportionate) {
90+
super(imageData, description, proportionate);
91+
}
92+
93+
/**
94+
* Creates a <CODE>ShrinkIcon</CODE> from the image.
95+
*
96+
* @param image the image
97+
*
98+
* @see ImageIcon#ImageIcon(java.awt.Image)
99+
*/
100+
public ShrinkIcon(Image image) {
101+
super(image);
102+
}
103+
104+
/**
105+
* Creates a <CODE>ShrinkIcon</CODE> from the image with the specified behavior.
106+
*
107+
* @param image the image
108+
* @param proportionate <code>true</code> to retain the image's aspect ratio,
109+
* <code>false</code> to allow distortion of the image to fit the
110+
* component.
111+
*
112+
* @see ImageIcon#ImageIcon(java.awt.Image)
113+
*/
114+
public ShrinkIcon(Image image, boolean proportionate) {
115+
super(image, proportionate);
116+
}
117+
118+
/**
119+
* Creates a <CODE>ShrinkIcon</CODE> from the image.
120+
*
121+
* @param image the image
122+
* @param description a brief textual description of the image
123+
*
124+
* @see ImageIcon#ImageIcon(java.awt.Image, java.lang.String)
125+
*/
126+
public ShrinkIcon(Image image, String description) {
127+
super(image, description);
128+
}
129+
130+
/**
131+
* Creates a <CODE>ShrinkIcon</CODE> from the image with the specified behavior.
132+
*
133+
* @param image the image
134+
* @param description a brief textual description of the image
135+
* @param proportionate <code>true</code> to retain the image's aspect ratio,
136+
* <code>false</code> to allow distortion of the image to fit the
137+
* component.
138+
*
139+
* @see ImageIcon#ImageIcon(java.awt.Image, java.lang.String)
140+
*/
141+
public ShrinkIcon(Image image, String description, boolean proportionate) {
142+
super(image, description, proportionate);
143+
}
144+
145+
/**
146+
* Creates a <CODE>ShrinkIcon</CODE> from the specified file.
147+
*
148+
* @param filename a String specifying a filename or path
149+
*
150+
* @see ImageIcon#ImageIcon(java.lang.String)
151+
*/
152+
public ShrinkIcon(String filename) {
153+
super(filename);
154+
}
155+
156+
/**
157+
* Creates a <CODE>ShrinkIcon</CODE> from the specified file with the specified behavior.
158+
*
159+
* @param filename a String specifying a filename or path
160+
* @param proportionate <code>true</code> to retain the image's aspect ratio,
161+
* <code>false</code> to allow distortion of the image to fit the
162+
* component.
163+
*
164+
* @see ImageIcon#ImageIcon(java.lang.String)
165+
*/
166+
public ShrinkIcon(String filename, boolean proportionate) {
167+
super(filename, proportionate);
168+
}
169+
170+
/**
171+
* Creates a <CODE>ShrinkIcon</CODE> from the specified file.
172+
*
173+
* @param filename a String specifying a filename or path
174+
* @param description a brief textual description of the image
175+
*
176+
* @see ImageIcon#ImageIcon(java.lang.String, java.lang.String)
177+
*/
178+
public ShrinkIcon(String filename, String description) {
179+
super(filename, description);
180+
}
181+
182+
/**
183+
* Creates a <CODE>ShrinkIcon</CODE> from the specified file with the specified behavior.
184+
*
185+
* @param filename a String specifying a filename or path
186+
* @param description a brief textual description of the image
187+
* @param proportionate <code>true</code> to retain the image's aspect ratio,
188+
* <code>false</code> to allow distortion of the image to fit the
189+
* component.
190+
*
191+
* @see ImageIcon#ImageIcon(java.awt.Image, java.lang.String)
192+
*/
193+
public ShrinkIcon(String filename, String description, boolean proportionate) {
194+
super(filename, description, proportionate);
195+
}
196+
197+
/**
198+
* Creates a <CODE>ShrinkIcon</CODE> from the specified URL.
199+
*
200+
* @param location the URL for the image
201+
*
202+
* @see ImageIcon#ImageIcon(java.net.URL)
203+
*/
204+
public ShrinkIcon(URL location) {
205+
super(location);
206+
}
207+
208+
/**
209+
* Creates a <CODE>ShrinkIcon</CODE> from the specified URL with the specified behavior.
210+
*
211+
* @param location the URL for the image
212+
* @param proportionate <code>true</code> to retain the image's aspect ratio,
213+
* <code>false</code> to allow distortion of the image to fit the
214+
* component.
215+
*
216+
* @see ImageIcon#ImageIcon(java.net.URL)
217+
*/
218+
public ShrinkIcon(URL location, boolean proportionate) {
219+
super(location, proportionate);
220+
}
221+
222+
/**
223+
* Creates a <CODE>ShrinkIcon</CODE> from the specified URL.
224+
*
225+
* @param location the URL for the image
226+
* @param description a brief textual description of the image
227+
*
228+
* @see ImageIcon#ImageIcon(java.net.URL, java.lang.String)
229+
*/
230+
public ShrinkIcon(URL location, String description) {
231+
super(location, description);
232+
}
233+
234+
/**
235+
* Creates a <CODE>ShrinkIcon</CODE> from the specified URL with the specified behavior.
236+
*
237+
* @param location the URL for the image
238+
* @param description a brief textual description of the image
239+
* @param proportionate <code>true</code> to retain the image's aspect ratio,
240+
* <code>false</code> to allow distortion of the image to fit the
241+
* component.
242+
*
243+
* @see ImageIcon#ImageIcon(java.net.URL, java.lang.String)
244+
*/
245+
public ShrinkIcon(URL location, String description, boolean proportionate) {
246+
super(location, description, proportionate);
247+
}
248+
249+
/**
250+
* Paints the icon. If necessary, the image is reduced to fit the component to
251+
* which it is painted, otherwise the image is centered horizontally and/or
252+
* vertically and painted with a width and height not exceeding its natural size.
253+
* <P>
254+
* If the proportion has not been specified, or has been specified as
255+
* <code>true</code>, the aspect ratio of the image will be preserved when
256+
* reducing the size, by padding and centering the image horizontally or
257+
* vertically.
258+
* <P>
259+
* If the proportion has been specified as <code>false</code> the image may be
260+
* reduced on one or both axes, each independent of the other, to fit the component.
261+
* <P>
262+
* If this icon has no image observer,this method uses the <code>c</code> component
263+
* as the observer.
264+
*
265+
* @param c the component to which the Icon is painted. This is used as the
266+
* observer if this icon has no image observer
267+
* @param g the graphics context
268+
* @param x not used
269+
* @param y not used
270+
*
271+
* @see StretchIcon#paintIcon(java.awt.Component, java.awt.Graphics, int, int)
272+
*/
273+
@Override
274+
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
275+
Image image = getImage();
276+
if (image == null) {
277+
return;
278+
}
279+
Insets insets = ((Container) c).getInsets();
280+
x = insets.left;
281+
y = insets.top;
282+
283+
int w = c.getWidth() - x - insets.right;
284+
int h = c.getHeight() - y - insets.bottom;
285+
286+
int iw = image.getWidth(c);
287+
int ih = image.getHeight(c);
288+
289+
if (proportionate && (w < iw || h < ih)) {
290+
super.paintIcon(c, g, x, y);
291+
} else {
292+
if (w > iw) {
293+
x += (w - iw) / 2;
294+
}
295+
if (h > ih) {
296+
y += (h - ih) / 2;
297+
}
298+
299+
ImageObserver io = getImageObserver();
300+
g.drawImage(image, x, y, io == null ? c : io);
301+
}
302+
}
303+
}

0 commit comments

Comments
 (0)