Java Reference
In-Depth Information
11.3.3 ImageIcon
A convenient shortcut is to use ImageIcon to load an image. This class, which
came in version 1.2 with the javax.swing package, is nominally for obtaining
a small image to use as an icon on a button and other component. However, it can
be used to load any image file of any size. The class contains a MediaTracker
internally, so it provides a compact way to load a single image with just a couple
of lines of code:
...
ImageIcon img - icon = new ImageIcon (url);
Image image = img - icon.getImage ();
...
Here the constructor blocks until either the image is loaded or the address is
deemed invalid. In the latter case, an image icon with zero dimensions is created.
The method
int getImageLoadStatus ()
returns the MediaTracker status value to indicate whether the loading was
successful. Note that the internal MediaTracker object is a static property
used by all the ImageIcon instances.
11.3.4 Reading a BufferedImage
The javax.imageio package, which appeared in Java 1.4, includes the
ImageIO class that offers several methods for reading and writing images in
encoded formats. For example, the methods
public static BufferedImage read (File input)
public static BufferedImage read (URL input)
read an image from a given file or URL address and return it as a
BufferedImage object. If loading from a particular URL might cause a sub-
stantial delay then it would be wise to use the image-loading techniques discussed
above and then convert the Image to a BufferedImage (see Section 11.5).
As of Java 5.0 the image encodings JPEG, GIF, PNG, BMP, and WBMP
(Wireless bitmap) can be read with these methods. Implementations on some
platforms may offer additional types. The ImageIO class includes several meth-
ods to determine what file types are available. For example,
String[] format - names = ImageIO.getReaderFormatNames ();
provides a string array that would include “gif ”, “jpeg”, “png”, “bmp”, and
“wbmp” types. This method
format - names = ImageIO.getReaderMIMETypes ();
returns strings in the MIME format such as “image/jpeg” and “image/png”.
Search WWH ::




Custom Search