Applets can handle both images and audio. As we write this, images must be in GIF or JPEG form, audio files in AU, AIFF, WAV, or MIDI. Animated GIFs are ok, and the animation is displayed. Usually the files containing this information are specified as a URL, so we take URLs up first.
A URL is really nothing more than a description of a resource on the Internet. For example, "http://java.oracle.com/index.html" tells the browser to use the hypertext transfer protocol on the file index.html located at java.oracle.com. Java has the class URL that encapsulates URLs. The simplest way to make a URL is to give a string to the URL constructor:
URL u = new URL("http://java.oracle.com/index.html");
This is called an absolute URL because we specify the entire resource name. Another useful URL constructor is a relative URL.
URL data = new URL(u, "data/planets.dat");
This specifies the file planets.dat, located in the data subdirectory of the URL u. Both constructors make sure that you have used the correct syntax for a URL. If you haven't, they cause a MalformedURLException. This is one of the exceptions that the compiler will not let you ignore. The relevant code is as follows:
String s = "http://java.oracle.com/index.html";
URL u = new URL(s);
. . .
// deal with error
We will discuss this syntax for dealing with exceptions in detail in . Until then, if you see code like this in one of our code samples, just gloss over the try and catch keywords. A common way of obtaining a URL is to ask an applet where it came from, in particular:
- What is the URL of the page that is calling it?
- What is the URL of the applet itself?
To find the former, use the getDocumentBase method; to find the latter, use getCodeBase.
Either the documentation or the implementation of the getDocumentBase and getCodeBase methods is wrong—see bug #4456393 on the Java bug parade. The getDocumentBase method returns the URL of the actual HTML document with the applet tag, even though the API documentation claims that it returns the URL of the directory only. The getCodeBase method returns the directory that contains the applet class, even though the API documentation states that it returns "the URL of the applet itself." The bug report itself wrongly claims that the two methods should be switched. That does not make sense either: if you use a codebase attribute, then the getCodeBase method correctly reports the code directory, which is different from the document directory.
Obtaining Multimedia Files
You can retrieve images and audio files with the getImage and getAudioClip methods. For example:
Image cat = getImage(getCodeBase(), "images/cat.gif");
AudioClip meow = getAudioClip(getDocumentBase(),
Here, we use the getDocumentBase method that returns the URL from which your applet is loaded. The second argument to the URL constructor specifies where the image or audio clip is located, relative to the base document. (Applets do not need to go through a Toolkit object to get an image.)
Once you have the images and audio clips, what can you do with them? You saw in how to display a single image. In the multithreading chapter of Volume 2, you will see how to play an animation sequence composed of multiple images. To play an audio clip, simply invoke its play method. You can also call play without first loading the audio clip.
The images and audio clips must be located on the same server that hosts the applet code. For security reasons, applets cannot access files on another server ("applets can only phone home").
However, to show an image, you must first load it. For faster downloading, multimedia objects can be stored in JAR files (see the section below). The getImage and getAudioClip/play methods automatically search the JAR files of the applet. If the image or audio file is contained in a JAR file, it is loaded immediately. Otherwise, the browser requests it from the web server.
- URL(String name)
creates a URL object from a string describing an absolute URL.
- URL(URL base, String name)
creates a relative URL object. If the string name describes an absolute URL, then the base URL is ignored. Otherwise, it is interpreted as a relative directory from the base URL.
- URL getDocumentBase()
According to the API documentation, this method gets the URL for the directory of the page that contains the applet. Actually, as of SDK 1.4, it gets the URL of the page. See bug #4456393 on the Java bug parade.
- URL getCodeBase()
According to the API documentation, this method gets the URL of the applet class. As of SDK 1.4, it gets the directory of that URL. See bug #4456393 on the Java bug parade.
- void play(URL url)
- void play(URL url, String name)
The first form plays an audio file specified by the URL. The second form uses the string to provide a path relative to the URL in the first argument. Nothing happens if the audio clip cannot be found.
- AudioClip getAudioClip(URL url)
- AudioClip getAudioClip(URL url, String name)
The first form gets an audio clip, given a URL. The second form uses the string to provide a path relative to the URL in the first argument. The methods return null if the audio clip cannot be found.
- Image getImage(URL url)
- Image getImage(URL url, String name)
return an image object that encapsulates the image specified by the URL. If the image does not exist, this method immediately returns null. Otherwise, a separate thread is launched to load the image. See for details on image acquisition.