55 |
|
|
56 |
/** |
/** |
57 |
* An {@link Icon} implementation that is backed by an {@link Image}. |
* An {@link Icon} implementation that is backed by an {@link Image}. |
|
* |
|
|
* @author original author unknown |
|
58 |
*/ |
*/ |
59 |
public class ImageIcon |
public class ImageIcon |
60 |
implements Icon, Serializable, Accessible |
implements Icon, Serializable, Accessible |
222 |
/** The AccessibleContext of this ImageIcon. */ |
/** The AccessibleContext of this ImageIcon. */ |
223 |
private AccessibleContext accessibleContext; |
private AccessibleContext accessibleContext; |
224 |
|
|
225 |
|
/** |
226 |
|
* Creates an ImageIcon without any properties set. |
227 |
|
*/ |
228 |
public ImageIcon() |
public ImageIcon() |
229 |
{ |
{ |
230 |
} |
} |
231 |
|
|
232 |
public ImageIcon(String file) |
/** |
233 |
|
* Constructs an ImageIcon given a filename. The icon's description |
234 |
|
* is initially set to the filename itself. A filename of "" means |
235 |
|
* create a blank icon. |
236 |
|
* |
237 |
|
* @param filename name of file to load or "" for a blank icon |
238 |
|
*/ |
239 |
|
public ImageIcon(String filename) |
240 |
{ |
{ |
241 |
this(file, file); |
this(filename, filename); |
242 |
} |
} |
243 |
|
|
244 |
public ImageIcon(String file, String description) |
/** |
245 |
|
* Constructs an ImageIcon from the given filename, setting its |
246 |
|
* description to the given description. A filename of "" means |
247 |
|
* create a blank icon. |
248 |
|
* |
249 |
|
* @param filename name of file to load or "" for a blank icon |
250 |
|
* @param description human-readable description of this icon |
251 |
|
*/ |
252 |
|
public ImageIcon(String filename, String description) |
253 |
{ |
{ |
254 |
this(Toolkit.getDefaultToolkit().getImage(file), description); |
this(Toolkit.getDefaultToolkit().getImage(filename), description); |
255 |
} |
} |
256 |
|
|
257 |
|
/** |
258 |
|
* Creates an ImageIcon from the given byte array without any |
259 |
|
* description set. |
260 |
|
*/ |
261 |
public ImageIcon(byte[] imageData) |
public ImageIcon(byte[] imageData) |
262 |
{ |
{ |
263 |
this(imageData, null); |
this(imageData, null); |
264 |
} |
} |
265 |
|
|
266 |
|
/** |
267 |
|
* Creates an ImageIcon from the given byte array and sets the given |
268 |
|
* description. |
269 |
|
*/ |
270 |
public ImageIcon(byte[] imageData, String description) |
public ImageIcon(byte[] imageData, String description) |
271 |
{ |
{ |
272 |
this(Toolkit.getDefaultToolkit().createImage(imageData), description); |
this(Toolkit.getDefaultToolkit().createImage(imageData), description); |
273 |
} |
} |
274 |
|
|
275 |
|
/** |
276 |
|
* Creates an ImageIcon from the given URL without any description |
277 |
|
* set. |
278 |
|
*/ |
279 |
public ImageIcon(URL url) |
public ImageIcon(URL url) |
280 |
{ |
{ |
281 |
this(url, null); |
this(url, null); |
282 |
} |
} |
283 |
|
|
284 |
|
/** |
285 |
|
* Creates an ImageIcon from the given URL and sets the given |
286 |
|
* description. |
287 |
|
*/ |
288 |
public ImageIcon(URL url, String description) |
public ImageIcon(URL url, String description) |
289 |
{ |
{ |
290 |
this(Toolkit.getDefaultToolkit().getImage(url), description); |
this(Toolkit.getDefaultToolkit().getImage(url), description); |
291 |
} |
} |
292 |
|
|
293 |
|
/** |
294 |
|
* Creates an ImageIcon from the given Image without any description |
295 |
|
* set. |
296 |
|
*/ |
297 |
public ImageIcon(Image image) |
public ImageIcon(Image image) |
298 |
{ |
{ |
299 |
this(image, null); |
this(image, null); |
300 |
} |
} |
301 |
|
|
302 |
|
/** |
303 |
|
* Creates an ImageIcon from the given Image and sets the given |
304 |
|
* description. |
305 |
|
*/ |
306 |
public ImageIcon(Image image, String description) |
public ImageIcon(Image image, String description) |
307 |
{ |
{ |
308 |
setImage(image); |
setImage(image); |
309 |
setDescription(description); |
setDescription(description); |
310 |
} |
} |
311 |
|
|
312 |
|
/** |
313 |
|
* Returns the ImageObserver that is used for all Image |
314 |
|
* operations. Defaults to null when not explicitly set. |
315 |
|
*/ |
316 |
public ImageObserver getImageObserver() |
public ImageObserver getImageObserver() |
317 |
{ |
{ |
318 |
return observer; |
return observer; |
319 |
} |
} |
320 |
|
|
321 |
|
/** |
322 |
|
* Sets the ImageObserver that will be used for all Image |
323 |
|
* operations. Can be set to null (the default) when no observer is |
324 |
|
* needed. |
325 |
|
*/ |
326 |
public void setImageObserver(ImageObserver newObserver) |
public void setImageObserver(ImageObserver newObserver) |
327 |
{ |
{ |
328 |
observer = newObserver; |
observer = newObserver; |
329 |
} |
} |
330 |
|
|
331 |
|
/** |
332 |
|
* Returns the backing Image for this ImageIcon. Might be set to |
333 |
|
* null in which case no image is shown. |
334 |
|
*/ |
335 |
public Image getImage() |
public Image getImage() |
336 |
{ |
{ |
337 |
return image; |
return image; |
338 |
} |
} |
339 |
|
|
340 |
|
/** |
341 |
|
* Explicitly sets the backing Image for this ImageIcon. Will call |
342 |
|
* loadImage() to make sure that the Image is completely loaded |
343 |
|
* before returning. |
344 |
|
*/ |
345 |
public void setImage(Image image) |
public void setImage(Image image) |
346 |
{ |
{ |
347 |
loadImage(image); |
loadImage(image); |
348 |
this.image = image; |
this.image = image; |
349 |
} |
} |
350 |
|
|
351 |
|
/** |
352 |
|
* Returns a human readable description for this ImageIcon or null |
353 |
|
* when no description is set or available. |
354 |
|
*/ |
355 |
public String getDescription() |
public String getDescription() |
356 |
{ |
{ |
357 |
return description; |
return description; |
358 |
} |
} |
359 |
|
|
360 |
|
/** |
361 |
|
* Sets a human readable description for this ImageIcon. Can be set |
362 |
|
* to null when no description is available. |
363 |
|
*/ |
364 |
public void setDescription(String description) |
public void setDescription(String description) |
365 |
{ |
{ |
366 |
this.description = description; |
this.description = description; |
367 |
} |
} |
368 |
|
|
369 |
|
/** |
370 |
|
* Returns the the height of the backing Image, or -1 if the backing |
371 |
|
* Image is null. The getHeight() method of the Image will be called |
372 |
|
* with the set observer of this ImageIcon. |
373 |
|
*/ |
374 |
public int getIconHeight() |
public int getIconHeight() |
375 |
{ |
{ |
376 |
|
if (image == null) |
377 |
|
return -1; |
378 |
|
|
379 |
return image.getHeight(observer); |
return image.getHeight(observer); |
380 |
} |
} |
381 |
|
|
382 |
|
/** |
383 |
|
* Returns the the width of the backing Image, or -1 if the backing |
384 |
|
* Image is null. The getWidth() method of the Image will be called |
385 |
|
* with the set observer of this ImageIcon. |
386 |
|
*/ |
387 |
public int getIconWidth() |
public int getIconWidth() |
388 |
{ |
{ |
389 |
|
if (image == null) |
390 |
|
return -1; |
391 |
|
|
392 |
return image.getWidth(observer); |
return image.getWidth(observer); |
393 |
} |
} |
394 |
|
|
395 |
|
/** |
396 |
|
* Calls <code>g.drawImage()</code> on the backing Image using the |
397 |
|
* set observer of this ImageIcon. If the set observer is null, the |
398 |
|
* given Component is used as observer. |
399 |
|
*/ |
400 |
public void paintIcon(Component c, Graphics g, int x, int y) |
public void paintIcon(Component c, Graphics g, int x, int y) |
401 |
{ |
{ |
402 |
g.drawImage(image, x, y, observer != null ? observer : c); |
g.drawImage(image, x, y, observer != null ? observer : c); |