aboutsummaryrefslogtreecommitdiff
path: root/src/com/wordpress
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/wordpress')
-rw-r--r--src/com/wordpress/tipsforjava/swing/StretchIcon.java372
1 files changed, 372 insertions, 0 deletions
diff --git a/src/com/wordpress/tipsforjava/swing/StretchIcon.java b/src/com/wordpress/tipsforjava/swing/StretchIcon.java
new file mode 100644
index 0000000..667f1cd
--- /dev/null
+++ b/src/com/wordpress/tipsforjava/swing/StretchIcon.java
@@ -0,0 +1,372 @@
1/**
2 * @(#)StretchIcon.java 1.0 03/27/12
3 */
4package com.wordpress.tipsforjava.swing;
5
6import java.awt.Component;
7import java.awt.Container;
8import java.awt.Graphics;
9import java.awt.Image;
10import java.awt.Insets;
11import java.awt.image.ImageObserver;
12import java.net.URL;
13import javax.swing.ImageIcon;
14
15/**
16 * An <CODE>Icon</CODE> that scales its image to fill the component area,
17 * excluding any border or insets, optionally maintaining the image's aspect
18 * ratio by padding and centering the scaled image horizontally or vertically.
19 * <P>
20 * The class is a drop-in replacement for <CODE>ImageIcon</CODE>.
21 * <P>
22 * As the size of the Icon is determined by the size of the component in which
23 * it is displayed, <CODE>StretchIcon</CODE> must only be used in conjunction
24 * with a component and layout that does not depend on the size of the
25 * component's Icon.
26 *
27 * @version 1.0 03/22/12
28 * @author Darryl
29 */
30public class StretchIcon extends ImageIcon {
31
32 /**
33 * Determines whether the aspect ratio of the image is maintained. Set to
34 * <code>false</code> to distort the image to fill the component.
35 */
36 protected boolean proportionate = true;
37
38 /**
39 * Creates a <CODE>StretchIcon</CODE> from an array of bytes.
40 *
41 * @param imageData
42 * an array of pixels in an image format supported by the AWT
43 * Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
44 *
45 * @see ImageIcon#ImageIcon(byte[])
46 */
47 public StretchIcon(byte[] imageData) {
48 super(imageData);
49 }
50
51 /**
52 * Creates a <CODE>StretchIcon</CODE> from an array of bytes with the
53 * specified behavior.
54 *
55 * @param imageData
56 * an array of pixels in an image format supported by the AWT
57 * Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
58 * @param proportionate
59 * <code>true</code> to retain the image's aspect ratio,
60 * <code>false</code> to allow distortion of the image to fill
61 * the component.
62 *
63 * @see ImageIcon#ImageIcon(byte[])
64 */
65 public StretchIcon(byte[] imageData, boolean proportionate) {
66 super(imageData);
67 this.proportionate = proportionate;
68 }
69
70 /**
71 * Creates a <CODE>StretchIcon</CODE> from an array of bytes.
72 *
73 * @param imageData
74 * an array of pixels in an image format supported by the AWT
75 * Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
76 * @param description
77 * a brief textual description of the image
78 *
79 * @see ImageIcon#ImageIcon(byte[], java.lang.String)
80 */
81 public StretchIcon(byte[] imageData, String description) {
82 super(imageData, description);
83 }
84
85 /**
86 * Creates a <CODE>StretchIcon</CODE> from an array of bytes with the
87 * specified behavior.
88 *
89 * @see ImageIcon#ImageIcon(byte[])
90 * @param imageData
91 * an array of pixels in an image format supported by the AWT
92 * Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
93 * @param description
94 * a brief textual description of the image
95 * @param proportionate
96 * <code>true</code> to retain the image's aspect ratio,
97 * <code>false</code> to allow distortion of the image to fill
98 * the component.
99 *
100 * @see ImageIcon#ImageIcon(byte[], java.lang.String)
101 */
102 public StretchIcon(byte[] imageData, String description, boolean proportionate) {
103 super(imageData, description);
104 this.proportionate = proportionate;
105 }
106
107 /**
108 * Creates a StretchIcon from the image.
109 *
110 * @param image
111 * the image
112 *
113 * @see ImageIcon#ImageIcon(java.awt.Image)
114 */
115 public StretchIcon(Image image) {
116 super(image);
117 }
118
119 /**
120 * Creates a StretchIcon from the image with the specifiec behavior.
121 *
122 * @param image
123 * the image
124 * @param proportionate
125 * <code>true</code> to retain the image's aspect ratio,
126 * <code>false</code> to allow distortion of the image to fill
127 * the component.
128 *
129 * @see ImageIcon#ImageIcon(java.awt.Image)
130 */
131 public StretchIcon(Image image, boolean proportionate) {
132 super(image);
133 this.proportionate = proportionate;
134 }
135
136 /**
137 * Creates a StretchIcon from the image.
138 *
139 * @param image
140 * the image
141 * @param description
142 * a brief textual description of the image
143 *
144 * @see ImageIcon#ImageIcon(java.awt.Image, java.lang.String)
145 */
146 public StretchIcon(Image image, String description) {
147 super(image, description);
148 }
149
150 /**
151 * Creates a StretchIcon from the image with the specified behavior.
152 *
153 * @param image
154 * the image
155 * @param description
156 * a brief textual description of the image
157 * @param proportionate
158 * <code>true</code> to retain the image's aspect ratio,
159 * <code>false</code> to allow distortion of the image to fill
160 * the component.
161 *
162 * @see ImageIcon#ImageIcon(java.awt.Image, java.lang.String)
163 */
164 public StretchIcon(Image image, String description, boolean proportionate) {
165 super(image, description);
166 this.proportionate = proportionate;
167 }
168
169 /**
170 * Creates a StretchIcon from the specified file.
171 *
172 * @param filename
173 * a String specifying a filename or path
174 *
175 * @see ImageIcon#ImageIcon(java.lang.String)
176 */
177 public StretchIcon(String filename) {
178 super(filename);
179 }
180
181 /**
182 * Creates a StretchIcon from the specified file with the specified
183 * behavior.
184 *
185 * @param filename
186 * a String specifying a filename or path
187 * @param proportionate
188 * <code>true</code> to retain the image's aspect ratio,
189 * <code>false</code> to allow distortion of the image to fill
190 * the component.
191 *
192 * @see ImageIcon#ImageIcon(java.lang.String)
193 */
194 public StretchIcon(String filename, boolean proportionate) {
195 super(filename);
196 this.proportionate = proportionate;
197 }
198
199 /**
200 * Creates a StretchIcon from the specified file.
201 *
202 * @param filename
203 * a String specifying a filename or path
204 * @param description
205 * a brief textual description of the image
206 *
207 * @see ImageIcon#ImageIcon(java.lang.String, java.lang.String)
208 */
209 public StretchIcon(String filename, String description) {
210 super(filename, description);
211 }
212
213 /**
214 * Creates a StretchIcon from the specified file with the specified
215 * behavior.