Applet Basics

Applet Basics


Applets--programs that can run on World Wide Web pages--were the thing that made Java a computer magazine cover subject upon its release. Applets put a spotlight on the ways that Java was different and remarkable. Before Java, World Wide Web pages were a combination of text, images, and forms that used gateway programs running on the computer that hosted the pages. These gateway programs required special access to the Web page server machine, so most Web users did not have the ability to use them. Writing them required even more expertise.

In contrast, programmers of all skill levels can write Java applets, and you'll write several during the span of these 24 hours. You can test applets with any Web browser that handles Java programs and put one on a Web page without any special access from a Web provider. The Java programs that you toured during the previous hour were all applets. Their structure differs from applications in several important ways, and they are designed specifically for presentation on the World Wide Web.

As stated previously, applets do not have a main() block like applications do. Applets have several different sections that are handled depending on what is happening in the applet. These sections are detailed fully during Hour 13, "Learning How Applets Work." Two of the sections are the init() block statement and the paint() block. init() is short for initialization, and it is used to set up anything that needs to be set up as an applet first runs. The paint() block is used to display anything that should be displayed.

To see an applet version of the Root application, create a new file in your word processor and call it RootApplet.java. Enter the code in Listing 4.3; save it when you're done. Compile the file with the javac compiler tool by typing the following:

javac RootApplet.java


Listing 4.3. The full text of RootApplet.java.

 

 1: public class RootApplet extends java.applet.Applet {

 2:         int number;

 3:

 4:         public void init() {

 5:            number = 225;

 6:     }

 7:

 8:         public void paint(java.awt.Graphics g) {

 9:            g.drawString("The square root of " +

10:                           number +

11:                           " is " +

12:                   Math.sqrt(number), 5, 50);

13:     }

14: }


This program contains a lot of the same statements as the Java application that did the same thing. The main difference is in how it is organized--the main() block has been replaced with an init() block and a paint() block.

The sample programs in this hour are provided primarily to introduce you to the way Java programs are structured. Some aspects of these programs will be introduced fully later, so don't feel like you're falling behind. The main purpose of this hour is to get the programs to compile and see how they function when you run them.

Unlike applications, compiled Java applets cannot be tested using the java interpreter tool. You have to put them on a Web page and view that page in one of two ways:

·         Use a Web browser that can handle Java applets, such as the current versions of Netscape Navigator or Microsoft Internet Explorer.

·         Use the appletviewer tool that comes with the Java Developer's Kit.

To create a Web page that can display the RootApplet program, return to your word processor and create a new file. Enter Listing 4.4 in that file and save it as RootApplet.html.

Listing 4.4. The full text of RootApplet.html.

1: <applet code="RootApplet.class" height=100 width=300>

2: </applet> This Web page contains the bare minimum needed to display a Java applet on a Web page. The <APPLET> tag is used to specify that a Java program is being put on the page, the code attribute provides the name of the applet, and the height and width attributes describe the size of the applet's display area. These items will be described in detail during Hour 13.

For now, use the appletviewer tool to take a look at this page. Type the following at the command line:

appletviewer RootApplet.html

Figure 4.1 shows what the applet looks like using appletviewer.

Figure 4.1. <../art/04/04tja01.jpg> The RootApplet applet displayed with the appletviewer tool.

Sending Parameters to Applets

Java applets are never run from the command line, so you can't specify arguments the way you can with applications. Applets use a different way to receive information at the time the program is run. This information is called parameters, and you can send parameters through the HTML page that runs the applet. You have to use a special HTML tag for parameters called <PARAM>.

Load the file RootApplet.java back into your word processor. The init() block of the program should resemble the following:

public void init() {

    number = 225;

}

Replace these three lines with the following statements:

public void init() {

                String parameter = getParameter("NUMBER");

                if (parameter != null)

                        number = Integer.parseInt(parameter);

}

Save the file and then compile it by typing the following at the command line:

javac RootApplet.java

Before you can try this change out, you need to modify the Web page RootApplet.html so that it sends a parameter. Load the page into your word processor and add a line between the <APPLET> line and the </APPLET> line, so that the code resembles the following:

<applet code="RootApplet.class" height=100 width=300>

<param name="NUMBER" value=196>

</applet>

Save the file when you're done, and load the page using appletviewer again. The output should resemble Figure 4.2. Change the value of the VALUE attribute in the RootApplet.html file and run the program again. Try this several times, and you'll see that your program is now flexible enough to handle any number.

Figure 4.2. <../art/04/04tja02.jpg> The modified RootApplet program displayed with appletviewer.

You can use as many parameters as needed to customize the operation of an applet, as long as each has a different NAME attribute specified along with the <PARAM> tag.

Workshop: Viewing the Code Used to Run Applets

As a brief workshop to better familiarize yourself with the <APPLET> tag and how it can be used to alter the performance of an applet, visit this book's World Wide Web site at the following address:

<http://www.prefect.com/java24>

Visit this site using either the current version of Netscape Navigator or Microsoft Internet Explorer. Go to the section of the site labeled Hour 4 Showcase, and you'll be given a guided tour through several working examples of applets. On each of these pages, you can use a pull-down menu command to view the HTML tags that were used to create the page. With Navigator, the command is View | Document Source, and with Internet Explorer, the command is View | Source. Compare the parameters that are used with each applet to the way the applet runs.

Appendix C, "This Book's Web Site," describes other things you can do on this book's site. The Web site is intended as a complement to the material covered in this book and a way to find out about corrections, revisions, or other information that makes these 24 hours more productive.

Summary

During this hour, you got a chance to create both a Java application and an applet. These two types of programs have several important differences in the way they function and the way they are created. The next several hours will continue to focus on applications as you become more experienced as a Java programmer. Applications are easier to test because they don't require you to create a Web page to view them; they can be easier to create as well. The last several hours of the book focus on applets, however, because that's the area where beginning programmers are most likely to want to put their skills to work.

Post a Comment

Previous Post Next Post