img
// Display the banner.
public void paint(Graphics g) {
g.drawString(msg, 50, 30);
}
}
getDocumentBase( ) and getCodeBase( )
Often, you will create applets that will need to explicitly load media and text. Java will
allow the applet to load data from the directory holding the HTML file that started the
applet (the document base) and the directory from which the applet's class file was loaded
(the code base). These directories are returned as URL objects (described in Chapter 20) by
getDocumentBase( ) and getCodeBase( ). They can be concatenated with a string that names
the file you want to load. To actually load another file, you will use the showDocument( )
method defined by the AppletContext interface, discussed in the next section.
The following applet illustrates these methods:
// Display code and document bases.
import java.awt.*;
import java.applet.*;
import java.net.*;
/*
<applet code="Bases" width=300 height=50>
</applet>
*/
public class Bases extends Applet{
// Display code and document bases.
public void paint(Graphics g) {
String msg;
URL url = getCodeBase(); // get code base
msg = "Code base: " + url.toString();
g.drawString(msg, 10, 20);
url = getDocumentBase(); // get document base
msg = "Document base: " + url.toString();
g.drawString(msg, 10, 40);
}
}
Sample output from this program is shown here:
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home