Previous Section  < Day Day Up >  Next Section

The appletviewer Browser

The 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.

Watch Out!

The current versions of Mozilla, Netscape Navigator, and Microsoft Internet Explorer do not offer built-in support for Java applets. Support for the language is available as a browser plug-in from Sun Microsystems. The Java Plug-in from Sun can be used to run a Java applet in a browser in place of the browser's Java interpreter. The Plug-in is included in the Java 2 Runtime Environment, an interpreter for running Java Programs that is installed along with the Java Development Kit. If it isn't already present on your system, you can download it from Sun's website at http://www.java.com.


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:

  • The Restart and Reload options are used to restart the execution of the applet. The difference between these two options is that Restart does not unload the applet before restarting it, whereas Reload does. The Reload option is equivalent to closing the applet viewer and opening it up again on the same web page.

  • The Start and Stop options are used to call the start() and stop() methods of the applet directly.

  • The Clone option creates a second copy of the same applet running in its own window.

  • The Tag option displays the program's APPLET or OBJECT tag, along with the HTML for any PARAM tags that configure the applet.

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.java

 1: 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:

  • Line 13 defines the return type of the method as a two-dimensional array of String objects.

  • Line 14 creates an array of String objects with three elements: "Name", "String", and "Programmer's Name". These elements describe one of the parameters that can be defined for the AppInfo applet. They describe the name of the parameter (Name in this case), the type of data that the parameter will hold (a string), and a description of the parameter ("Programmer's Name"). The three-element array is stored in the p1 object.

  • Lines 15–16 define two more String arrays for the Date and Version parameters.

  • Line 17 uses the response object to store an array that contains three string arrays: p1, p2, and p3.

  • Line 18 uses the response object as the method's return value.

Listing B.2 contains a web page that can be used to load the AppInfo applet.

Listing B.2. The Full Text of AppInfo.html

1: <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.

    Previous Section  < Day Day Up >  Next Section