18.3. Text I-O vs. Binary I-O

 
[Page 557 ( continued )]

16.9. (Optional) Locating Resource Using the URL Class

You have used the ImageIcon class to create an icon from an image file and used the setIcon method or the constructor to place the icon in a GUI component, such as a button or a label. For example, the following statements create an ImageIcon and set it on a JLabel object jlbl :


[Page 558]
  
[Page 558]
ImageIcon imageIcon = new ImageIcon( "c:\book\image\us.gif" ); jlbl.setIcon(imageIcon);

This approach presents a problem. The file location is fixed, because it uses the absolute file path on Windows. As a result, the program cannot run on other platforms and cannot run as an applet. Assume that image/us.gif is under the class directory. You can circumvent this problem by using a relative path, as follows :

 ImageIcon imageIcon =   new   ImageIcon(   "image/us.gif"   ); 

This works fine with Java applications on all platforms, but does not work with Java applets because applets cannot load local files. To make it to work with both applications and applets, you need to locate the file using the URL class.

The java.net.URL class can be used to identify files (image, audio, text, etc.) on the Internet. In general, a URL (Uniform Resource Locator) is a pointer to a "resource" on the World Wide Web on a local machine or a remote host. A resource can be something as simple as a file or a directory.

An URL for a file can also be accessed by class code in a way that is independent of the location of the file as long as the file is located in the class directory. Recall that the class directory is where the class (i.e., the .class file) is stored. For example, all the classes in this book are stored in c:\book. So the class directory is c:\book .

As discussed in §9.13.5, "The getClass Method," when a class is loaded, the JVM creates a meta-object for the class, which can be obtained using

 java.lang.Class metaObject =   this   .getClass(); 

The Class class provides access to useful information about the class, such as the data fields, constructors, and methods . It also contains the getResource(filename) method, which can be used to obtain the URL of a given file name in the class directory.

To obtain the URL of a file in the class directory, use

 URL url = metaObject.getResource(filename); 

For example, if the class directory is c:\book , the following statements create a URL for c:\book\image\us.gif :

 Class metaObject =   this   .getClass(); URL url = metaObject.getResource(   "image/us.gif"   ); 

You can now create an ImageIcon using

 ImageIcon imageIcon =   new   ImageIcon(url); 

Listing 16.11 gives the code that displays an image from image/us.gif in the class directory. The file image/us.gif is under the class directory, and its URL is obtained using the getResource method (line 5). A label with an image icon is created in line 6. The image icon is obtained from the URL.

Listing 16.11. DisplayImageWithURL.java
 1   import   javax.swing.*; 2 3   public class   DisplayImageWithURL   extends   JApplet { 4   public   DisplayImageWithURL() { 5     java.net.URL url =    this   .getClass().getResource(   "image/us.gif"   )  ; 6     add(   new   JLabel(    new   ImageIcon(url)  )); 7   } 8 } 


[Page 559]

If you replace the code in lines 5 “6 with the following code,

 add(   new   JLabel(   new   ImageIcon(   "image/us.gif"   ))); 

you can still run the program standalone, but not from a browser.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net