We only considered one ImageIcon constructor when we read icons for our toolbars buttons from a file. That constructor happened to accept a String argument that specified the file name. There are a number of other ImageIcon constructors, so here's the complete set:

An ImageIcon object contains a reference to an object of type Image as a member. The Image class is an abstract class that is a superclass of all classes that represent images in Java, so a reference of type Image can refer to any instance of a graphical image being used. You can obtain a reference to the Image member of an ImageIcon object by calling its getImage() method. An object of type Image can be created from data in GIF (Graphics Interchange Format), PNG (Portable Network Graphics), or JPEG (Joint Photographic Experts Group) format. So, whether the image data is passed to the constructor as an array - as is the case for two of the ImageIcon constructors, or is obtained from an external source such as a file or a URL object, it must be a GIF, PNG, or JPEG representation of an image.
While the GIF format for images is very common, it has some limitations. A GIF image can contain a maximum of 256 different colors, albeit selected from a palette of 16 million colors. The JPEG image format has been designed to store photographs, and is a much more sophisticated way of representing a color image. The sophistication comes at a price though. The complexity of the format and the data compression technique used means that it takes much longer to process than a GIF image, but it does make it feasible to transmit good quality photographic images over the net. The JPEG compression technique is also 'lossy', so the quality is not as good as the original image. The PNG image format is also much more flexible than GIF images and it stores images in a lossless form. It is designed to be a portable image storage form for computer-originated images. You can represent grayscale, indexed-color and true color images in PNG format, and you can also include an alpha channel that determines the transparency of the image when it is combined with others.
The constructors that create the ImageIcon object by referencing a String object specifying a file name, or by referencing a URL object defining an Internet source, always construct the internal Image object before returning. When you specify a URL as the source of the image data, there can be a considerable delay before the ImageIcon object creation is completed, depending on how long it takes to retrieve the image data from the source. Being able to create an ImageIcon object from a URL object is particularly relevant to applets. You can retrieve any images, or indeed any other external data that you use in your applet from an Internet source. Since URLs are so important, let's take a brief detour into how URLs and the URL class work.
Identifying Sources on a Network
Data sources on a network are identified by a Uniform Resource Locator, or URL. A source identified by a URL can be a file, or can be some other facility that makes data available, such as a search engine for instance. A URL is a relatively straightforward extension of the normal way of identifying a data source file by the path. It just has some extra bits to identify the computer that contains the source, and how you should communicate with the computer to get at the file. A URL is made up of four pieces of information, some of which may be omitted entirely or take default values:

The protocol identifies the particular communications method that is to be used to get at the file. HTTP is the HyperText Transfer Protocol used on the World Wide Web, FTP is the File Transfer Protocol, and there are others (we'll see one in the next chapter).
The domain name identifies the data source uniquely. The last part of the name identifies the kind of organization that owns the computer. For instance, a domain name ending in .com is usually a commercial organization in the USA such as Wrox Press or Sun, .co.uk is a UK company, .edu is an educational establishment in the USA, .ac.uk is an academic or research organization in the UK and .gov is a government or other public body. These are just a few examples. There are lots of others.
A port is just a number identifying a particular service on a computer. A computer can have many ports - each providing a different service. You don't usually need to specify the port number in a URL because a standard port number, which is typically associated with a particular protocol, will be assigned by default. The port number for the http protocol is 80 for example, and the port number for ftp is 21.
Because the domain name is part of the reference to a data source, every data source in a network has a unique URL. As long as you know the URL for the source of the data you want on the Internet, and the machine containing it is connected and active, you can go straight to it. Assuming, of course, you have permission!
The URL Class
As you saw when we discussed ImageIcon constructors, the URL class encapsulates URLs. This class is defined in the java.net package, so you will need an import statement for the package or for the class when you refer to the URL class in your code. A URL object identifies a particular URL and provides you with the tools to access it and read it, wherever it is on the network. You can create a URL object from a string specifying the URL, for example:
URL sourceURL = new URL("http://www.wrox.com/");
This defines a URL object corresponding to the home (or default) page on the Wrox web site.
Perhaps the most commonly used URL constructor accepts two arguments - a URL object identifying an Internet source, plus a String argument which is a specification for a source within the context of the URL specified by the first argument. The popularity of this form is due to the definition within the Applet class of a method, getCodeBase(), which returns the URL for the source where the .class file for the applet is stored. This enables you to create the URL for a file, picture.gif say, which is stored in the same directory as the applet code, wherever it is, with a statement such as:
URL getThePicture = new URL(getCodeBase(), "picture.gif");
There is another method in the Applet class (and therefore inherited in the JApplet class) that provides a useful URL object. This is the getDocumentBase() method that returns a URL object corresponding to the URL for the document (the .html file in other words) that contains the applets. This may well be different from the location of the applet itself. You could call this method to obtain the URL like this:
URL doc = getDocumentBase();
// Get the document containing the applet
You can also define a URL object by supplying a constructor with separate values for the protocol, the host name, the port number and the source name. You specify the port number as type int, and the other arguments as type String. The web site defined by the domain name "www.ncsa.uiuc.edu" contains a page that explains what a URL is. The file path and name for the page is "/demoweb/url-primer.html". You could define a URL object for this page with the statement:
URL sourceURL = new URL("http", // Protocol
"www.ncsa.uiuc.edu", // Host name
"/demoweb/url-primer.html"); // File
This uses the default port number for the protocol. You just need to supply the protocol, the domain name and the file path as String objects. Note that URL paths are always specified using forward slashes as separators, regardless of the type of server hosting the data source.
There is an alternative constructor defined in the URL class:
URL sourceURL = new URL("http", // Protocol
"www.ncsa.uiuc.edu", // Host name
80, // Port number
"/demoweb/url-primer.html"); // File
The value of 80 for the port number is the standard port number for http. The value of -1 for the port name selects the default port number for the protocol. If you specify a protocol that cannot be identified, a MalformedURLException will be thrown.
Once you have a URL object, you can get the components of the URL by calling the getProtocol(), getHost(), getPort() and getFile() methods for the object. The getPort() method returns the port number as type int, and the other methods return objects of type String.
The URL class also implements an equals() method that you can use to compare two URL objects. The expression URL1.equals(URL2) will return true if URL1 and URL2 specify the same file on the same host via the same port number and using the same protocol.
Perhaps the most useful method in the URL class is the openStream() method. This returns an object of type InputStream, which you can use to read the file represented by the URL. We can try this out by reading the file referenced by the sourceURL object we created in the code fragment above.
Continued...