19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

692 Chapter 18 Applets and Multimedia<br />

The java.net.URL class was used <strong>to</strong> locate a text file on the Internet in Section 14.13. It<br />

can also be used <strong>to</strong> locate image files and audio files on the Internet. In general, a URL object<br />

is a pointer <strong>to</strong> a “resource” on a local machine or a remote host. A resource can be a file or a<br />

direc<strong>to</strong>ry.<br />

The URL class can be used <strong>to</strong> locate a resource file from a class in a way that is independent<br />

of the file’s location, as long as the resource file is located in the class direc<strong>to</strong>ry. Recall that<br />

the class direc<strong>to</strong>ry is where the class is s<strong>to</strong>red. To obtain the URL object for a file from a class,<br />

use the following statement in the applet or application:<br />

Direc<strong>to</strong>ry<br />

An applet or<br />

application<br />

A resource file<br />

.<br />

.<br />

.<br />

Class metaObject = this.getClass();<br />

URL url = metaObject.getResource(resourceFilename);<br />

.<br />

.<br />

.<br />

meta object<br />

The getClass() method returns an instance of the java.lang.Class class for the current<br />

class. This instance is au<strong>to</strong>matically created by the JVM for every class loaded in<strong>to</strong> the memory.<br />

This instance, also known as a meta object, contains the information about the class file<br />

such as class name, construc<strong>to</strong>rs, and methods. You can obtain a URL object for a file in the<br />

class path by invoking the getResource(filename) method on the meta object. For example,<br />

if the class file is in c:\book, the following statements obtain a URL object for<br />

c:\book\image\us.gif.<br />

C:\book<br />

An applet or<br />

application<br />

image<br />

.<br />

.<br />

.<br />

Class metaObject = this.getClass();<br />

URL url = metaObject.getResource("image/us.gif");<br />

.<br />

.<br />

.<br />

us.gif<br />

You can now create an ImageIcon using<br />

ImageIcon imageIcon = new ImageIcon(url);<br />

Listing 18.11 gives the code that displays an image from image/us.gif in the class direc<strong>to</strong>ry.<br />

The file image/us.gif is under the class direc<strong>to</strong>ry, and its URL object is obtained using the<br />

getResource method (line 5). A label with an image icon is created in line 6. The image<br />

icon is obtained from the URL object.<br />

LISTING 18.11<br />

DisplayImageWithURL.java<br />

1 import javax.swing.*;<br />

2<br />

3 public class DisplayImageWithURL extends JApplet {

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!