|
| 1 | +/** |
| 2 | + * @(#)StretchIcon.java 1.0 03/27/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 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>, except that |
| 21 | + * the no-argument constructor is not supported. |
| 22 | + * <P> |
| 23 | + * As the size of the Icon is determined by the size of the component in which |
| 24 | + * it is displayed, <CODE>StretchIcon</CODE> must only be used in conjunction |
| 25 | + * with a component and layout that does not depend on the size of the |
| 26 | + * component's Icon. |
| 27 | + * |
| 28 | + * @version 1.0 03/27/12 |
| 29 | + * @author Darryl |
| 30 | + */ |
| 31 | +public class StretchIcon extends ImageIcon { |
| 32 | + |
| 33 | + /** |
| 34 | + * Determines whether the aspect ratio of the image is maintained. |
| 35 | + * Set to <code>false</code> to allow th image to distort to fill the component. |
| 36 | + */ |
| 37 | + protected boolean proportionate = true; |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates a <CODE>StretchIcon</CODE> from an array of bytes. |
| 41 | + * |
| 42 | + * @param imageData an array of pixels in an image format supported by |
| 43 | + * the AWT 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 specified behavior. |
| 53 | + * |
| 54 | + * @param imageData an array of pixels in an image format supported by |
| 55 | + * the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG |
| 56 | + * @param proportionate <code>true</code> to retain the image's aspect ratio, |
| 57 | + * <code>false</code> to allow distortion of the image to fill the |
| 58 | + * component. |
| 59 | + * |
| 60 | + * @see ImageIcon#ImageIcon(byte[]) |
| 61 | + */ |
| 62 | + public StretchIcon(byte[] imageData, boolean proportionate) { |
| 63 | + super(imageData); |
| 64 | + this.proportionate = proportionate; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates a <CODE>StretchIcon</CODE> from an array of bytes. |
| 69 | + * |
| 70 | + * @param imageData an array of pixels in an image format supported by |
| 71 | + * the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG |
| 72 | + * @param description a brief textual description of the image |
| 73 | + * |
| 74 | + * @see ImageIcon#ImageIcon(byte[], java.lang.String) |
| 75 | + */ |
| 76 | + public StretchIcon(byte[] imageData, String description) { |
| 77 | + super(imageData, description); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Creates a <CODE>StretchIcon</CODE> from an array of bytes with the specified behavior. |
| 82 | + * |
| 83 | + * @see ImageIcon#ImageIcon(byte[]) |
| 84 | + * @param imageData an array of pixels in an image format supported by |
| 85 | + * the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG |
| 86 | + * @param description a brief textual description of the image |
| 87 | + * @param proportionate <code>true</code> to retain the image's aspect ratio, |
| 88 | + * <code>false</code> to allow distortion of the image to fill the |
| 89 | + * component. |
| 90 | + * |
| 91 | + * @see ImageIcon#ImageIcon(byte[], java.lang.String) |
| 92 | + */ |
| 93 | + public StretchIcon(byte[] imageData, String description, boolean proportionate) { |
| 94 | + super(imageData, description); |
| 95 | + this.proportionate = proportionate; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Creates a <CODE>StretchIcon</CODE> from the image. |
| 100 | + * |
| 101 | + * @param image the image |
| 102 | + * |
| 103 | + * @see ImageIcon#ImageIcon(java.awt.Image) |
| 104 | + */ |
| 105 | + public StretchIcon(Image image) { |
| 106 | + super(image); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Creates a <CODE>StretchIcon</CODE> from the image with the specified behavior. |
| 111 | + * |
| 112 | + * @param image the image |
| 113 | + * @param proportionate <code>true</code> to retain the image's aspect ratio, |
| 114 | + * <code>false</code> to allow distortion of the image to fill the |
| 115 | + * component. |
| 116 | + * |
| 117 | + * @see ImageIcon#ImageIcon(java.awt.Image) |
| 118 | + */ |
| 119 | + public StretchIcon(Image image, boolean proportionate) { |
| 120 | + super(image); |
| 121 | + this.proportionate = proportionate; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Creates a <CODE>StretchIcon</CODE> from the image. |
| 126 | + * |
| 127 | + * @param image the image |
| 128 | + * @param description a brief textual description of the image |
| 129 | + * |
| 130 | + * @see ImageIcon#ImageIcon(java.awt.Image, java.lang.String) |
| 131 | + */ |
| 132 | + public StretchIcon(Image image, String description) { |
| 133 | + super(image, description); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Creates a <CODE>StretchIcon</CODE> from the image with the specified behavior. |
| 138 | + * |
| 139 | + * @param image the image |
| 140 | + * @param description a brief textual description of the image |
| 141 | + * @param proportionate <code>true</code> to retain the image's aspect ratio, |
| 142 | + * <code>false</code> to allow distortion of the image to fill the |
| 143 | + * component. |
| 144 | + * |
| 145 | + * @see ImageIcon#ImageIcon(java.awt.Image, java.lang.String) |
| 146 | + */ |
| 147 | + public StretchIcon(Image image, String description, boolean proportionate) { |
| 148 | + super(image, description); |
| 149 | + this.proportionate = proportionate; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Creates a <CODE>StretchIcon</CODE> from the specified file. |
| 154 | + * |
| 155 | + * @param filename a String specifying a filename or path |
| 156 | + * |
| 157 | + * @see ImageIcon#ImageIcon(java.lang.String) |
| 158 | + */ |
| 159 | + public StretchIcon(String filename) { |
| 160 | + super(filename); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Creates a <CODE>StretchIcon</CODE> from the specified file with the specified behavior. |
| 165 | + * |
| 166 | + * @param filename a String specifying a filename or path |
| 167 | + * @param proportionate <code>true</code> to retain the image's aspect ratio, |
| 168 | + * <code>false</code> to allow distortion of the image to fill the |
| 169 | + * component. |
| 170 | + * |
| 171 | + * @see ImageIcon#ImageIcon(java.lang.String) |
| 172 | + */ |
| 173 | + public StretchIcon(String filename, boolean proportionate) { |
| 174 | + super(filename); |
| 175 | + this.proportionate = proportionate; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Creates a <CODE>StretchIcon</CODE> from the specified file. |
| 180 | + * |
| 181 | + * @param filename a String specifying a filename or path |
| 182 | + * @param description a brief textual description of the image |
| 183 | + * |
| 184 | + * @see ImageIcon#ImageIcon(java.lang.String, java.lang.String) |
| 185 | + */ |
| 186 | + public StretchIcon(String filename, String description) { |
| 187 | + super(filename, description); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Creates a <CODE>StretchIcon</CODE> from the specified file with the specified behavior. |
| 192 | + * |
| 193 | + * @param filename a String specifying a filename or path |
| 194 | + * @param description a brief textual description of the image |
| 195 | + * @param proportionate <code>true</code> to retain the image's aspect ratio, |
| 196 | + * <code>false</code> to allow distortion of the image to fill the |
| 197 | + * component. |
| 198 | + * |
| 199 | + * @see ImageIcon#ImageIcon(java.awt.Image, java.lang.String) |
| 200 | + */ |
| 201 | + public StretchIcon(String filename, String description, boolean proportionate) { |
| 202 | + super(filename, description); |
| 203 | + this.proportionate = proportionate; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Creates a <CODE>StretchIcon</CODE> from the specified URL. |
| 208 | + * |
| 209 | + * @param location the URL for the image |
| 210 | + * |
| 211 | + * @see ImageIcon#ImageIcon(java.net.URL) |
| 212 | + */ |
| 213 | + public StretchIcon(URL location) { |
| 214 | + super(location); |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Creates a <CODE>StretchIcon</CODE> from the specified URL with the specified behavior. |
| 219 | + * |
| 220 | + * @param location the URL for the image |
| 221 | + * @param proportionate <code>true</code> to retain the image's aspect ratio, |
| 222 | + * <code>false</code> to allow distortion of the image to fill the |
| 223 | + * component. |
| 224 | + * |
| 225 | + * @see ImageIcon#ImageIcon(java.net.URL) |
| 226 | + */ |
| 227 | + public StretchIcon(URL location, boolean proportionate) { |
| 228 | + super(location); |
| 229 | + this.proportionate = proportionate; |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Creates a <CODE>StretchIcon</CODE> from the specified URL. |
| 234 | + * |
| 235 | + * @param location the URL for the image |
| 236 | + * @param description a brief textual description of the image |
| 237 | + * |
| 238 | + * @see ImageIcon#ImageIcon(java.net.URL, java.lang.String) |
| 239 | + */ |
| 240 | + public StretchIcon(URL location, String description) { |
| 241 | + super(location, description); |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * Creates a <CODE>StretchIcon</CODE> from the specified URL with the specified behavior. |
| 246 | + * |
| 247 | + * @param location the URL for the image |
| 248 | + * @param description a brief textual description of the image |
| 249 | + * @param proportionate <code>true</code> to retain the image's aspect ratio, |
| 250 | + * <code>false</code> to allow distortion of the image to fill the |
| 251 | + * component. |
| 252 | + * |
| 253 | + * @see ImageIcon#ImageIcon(java.net.URL, java.lang.String) |
| 254 | + */ |
| 255 | + public StretchIcon(URL location, String description, boolean proportionate) { |
| 256 | + super(location, description); |
| 257 | + this.proportionate = proportionate; |
| 258 | + } |
| 259 | + |
| 260 | + /** |
| 261 | + * Paints the icon. The image is reduced or magnified to fit the component to which |
| 262 | + * it is painted. |
| 263 | + * <P> |
| 264 | + * If the proportion has not been specified, or has been specified as <code>true</code>, |
| 265 | + * the aspect ratio of the image will be preserved by padding and centering the image |
| 266 | + * horizontally or vertically. Otherwise the image may be distorted to fill the |
| 267 | + * component it is painted to. |
| 268 | + * <P> |
| 269 | + * If this icon has no image observer,this method uses the <code>c</code> component |
| 270 | + * as the observer. |
| 271 | + * |
| 272 | + * @param c the component to which the Icon is painted. This is used as the |
| 273 | + * observer if this icon has no image observer |
| 274 | + * @param g the graphics context |
| 275 | + * @param x not used. |
| 276 | + * @param y not used. |
| 277 | + * |
| 278 | + * @see ImageIcon#paintIcon(java.awt.Component, java.awt.Graphics, int, int) |
| 279 | + */ |
| 280 | + @Override |
| 281 | + public synchronized void paintIcon(Component c, Graphics g, int x, int y) { |
| 282 | + Image image = getImage(); |
| 283 | + if (image == null) { |
| 284 | + return; |
| 285 | + } |
| 286 | + Insets insets = ((Container) c).getInsets(); |
| 287 | + x = insets.left; |
| 288 | + y = insets.top; |
| 289 | + |
| 290 | + int w = c.getWidth() - x - insets.right; |
| 291 | + int h = c.getHeight() - y - insets.bottom; |
| 292 | + |
| 293 | + if (proportionate) { |
| 294 | + int iw = image.getWidth(c); |
| 295 | + int ih = image.getHeight(c); |
| 296 | + |
| 297 | + if (iw * h < ih * w) { |
| 298 | + iw = (h * iw) / ih; |
| 299 | + x += (w - iw) / 2; |
| 300 | + w = iw; |
| 301 | + } else { |
| 302 | + ih = (w * ih) / iw; |
| 303 | + y += (h - ih) / 2; |
| 304 | + h = ih; |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + ImageObserver io = getImageObserver(); |
| 309 | + g.drawImage(image, x, y, w, h, io == null ? c : io); |
| 310 | + } |
| 311 | + |
| 312 | + /** |
| 313 | + * Overridden to return 0. The size of this Icon is determined by |
| 314 | + * the size of the component. |
| 315 | + * |
| 316 | + * @return 0 |
| 317 | + */ |
| 318 | + @Override |
| 319 | + public int getIconWidth() { |
| 320 | + return 0; |
| 321 | + } |
| 322 | + |
| 323 | + /** |
| 324 | + * Overridden to return 0. The size of this Icon is determined by |
| 325 | + * the size of the component. |
| 326 | + * |
| 327 | + * @return 0 |
| 328 | + */ |
| 329 | + @Override |
| 330 | + public int getIconHeight() { |
| 331 | + return 0; |
| 332 | + } |
| 333 | +} |
0 commit comments