< Day Day Up > |
The appletviewer BrowserThe appletviewer tool runs Java programs that require a web browser and are presented as part of an HTML document. It takes an HTML document as a command-line argument, as in the following example: appletviewer NewAuctions.html If the argument is a web address instead of a reference to a file, appletviewer will load the HTML document at that address. For example: appletviewer http://www.javaonthebrain.com Figure B.1 displays an applet loaded from this page, a site developed by cartoonist and Java game programmer Karl Hörnell. Figure B.1. Viewing Java web applets outside of a browser.When an HTML document is loaded by appletviewer, every applet on that document will begin running in its own window. The size of these windows depends on the HEIGHT and WIDTH attributes that were set in the applet's HTML tag. Unlike a web browser, appletviewer cannot be used to view the HTML document itself. If you want to see how the applet is laid out in relation to the other contents of the document, you must use a Java-capable web browser.
Using appletviewer is reasonably straightforward, but you may not be familiar with some of the menu options that are available as the viewer runs an applet. The following menu options are available:
Another option on the Applet pull-down menu is Info, which calls the getAppletInfo() and getParameterInfo() methods of the applet. A programmer can implement these methods to provide more information about the applet and the parameters that it can handle. The getAppletInfo() method will return a string that describes the applet. The getParameterInfo() method will return an array of string arrays that specify the name, type, and description of each parameter. Listing B.1 contains a Java 2 applet that demonstrates the use of these methods. Listing B.1. The Full Text of AppInfo.java1: import java.awt.*; 2: 3: public class AppInfo extends javax.swing.JApplet { 4: String name, date; 5: int version; 6: 7: public String getAppletInfo() { 8: String response = "This applet demonstrates the " 9: + "use of the Applet's Info feature."; 10: return response; 11: } 12: 13: public String[][] getParameterInfo() { 14: String[] p1 = { "Name", "String", "Programmer's name" }; 15: String[] p2 = { "Date", "String", "Today's date" }; 16: String[] p3 = { "Version", "int", "Version number" }; 17: String[][] response = { p1, p2, p3 }; 18: return response; 19: } 20: 21: public void init() { 22: name = getParameter("Name"); 23: date = getParameter("Date"); 24: String versText = getParameter("Version"); 25: if (versText != null) { 26: version = Integer.parseInt(versText); 27: } 28: } 29: 30: public void paint(Graphics screen) { 31: Graphics2D screen2D = (Graphics2D) screen; 32: screen2D.drawString("Name: " + name, 5, 50); 33: screen2D.drawString("Date: " + date, 5, 100); 34: screen2D.drawString("Version: " + version, 5, 150); 35: } 36: ] The main function of this applet is to display the value of three parameters: Name, Date, and Version. The getAppletInfo() method returns the following string: This applet demonstrates the use of the Applet's Info feature. The getParameterInfo() method is a bit more complicated if you haven't worked with multidimensional arrays. The following things are taking place:
Listing B.2 contains a web page that can be used to load the AppInfo applet. Listing B.2. The Full Text of AppInfo.html1: <applet code="AppInfo.class" height="200" width="170"> 2: <param name="Name" value="Rogers Cadenhead"> 3: <param name="Date" value="06/08/05"> 4: <param name="Version" value="4"> 5: </applet> Figure B.2 shows the applet running with the applet viewer, and Figure B.3 is a screen capture of the dialog box that opens when the viewer's Info menu option is selected. Figure B.2. The AppInfo applet running in appletviewer.Figure B.3. The Info dialog box of the AppInfo applet.These features require a browser that makes this information available to users. The JDK's appletviewer handles this through the Info menu option, but browsers such as Internet Explorer do not offer anything like it at this time. |
< Day Day Up > |