Core Java™ 2 Volume I - Fundamentals, Seventh Edition
Applets can handle both images and audio. As we write this, images must be in GIF, PNG, or JPEG form, audio files in AU, AIFF, WAV, or MIDI. Animated GIFs are supported, and the animation is displayed. Usually the files containing this information are specified as a URL, so we take up URLs first.
Encapsulating URLs
A URL is really nothing more than a description of a resource on the Internet. For example, "http://java.sun.com/index.html" tells the browser to use the hypertext transfer protocol on the file index.html located at java.sun.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.sun.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.
A common way of obtaining a URL is to ask an applet where it came from, in particular:
- What is the URL of the HTML page in which the applet is contained?
- What is the URL of the applet's codebase directory?
To find the former, use the getdocumentBase method; to find the latter, usegetCodeBase.
NOTE
In prior versions of the JDK, there was considerable confusion about these methods—see bug #4456393 on the Java bug parade (http://bugs.sun.com/bugdatabase/index.jsp). The documentation has finally been clarified in JDK 5.0. |
NOTE
You can access secure web pages (https URLs) from applets and through the Java Plug-in—seehttp://java.sun.com/products/plugin/1.3/docs/https.html. This uses the SSL capabilities of the underlying browser. |
Obtaining Multimedia Files
Image cat = getImage(getCodeBase(), "images/cat.gif"); AudioClip meow = getAudioClip(getCodeBase(), "audio/meow.au");
Here, we use the getCodeBase method that returns the URL from which your applet code is loaded. The second argument of the method calls specifies where the image or audio clip is located, relative to the base document.
NOTE
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"). |
You saw in Chapter 7 how to display an image. To play an audio clip, simply invoke itsplay method. You can also call the play method of the Applet class without first loading the audio clip.
play(getCodeBase(), "audio/meow.au");
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 the JAR file, it is loaded immediately. Otherwise, the browser requests it from the web server.
java.net.URL 1.0
- 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.
java.applet.Applet 1.0
- URL getDocumentBase()gets the URL of the web page containing this applet.
- URL getCodeBase()gets the URL of the codebase directory from which this applet is loaded. That is either the absolute URL of the directory referenced by the codebaseattribute or the directory of the HTML file if no codebase is specified.
- 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 parameter. 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 from the given 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, immediately returns null. Otherwise, a separate thread is launched to load the image.
0 comments:
Post a Comment