Previous Section  < Day Day Up >  Next Section

Applet Basics

When the Java language was introduced in 1995, the language feature that got the most attention was applets, Java programs that run on a World Wide Web page. Before Java, web pages were a combination of text, images, and forms that used Common Gateway Interface programs running on the computer that hosted the pages. These gateway programs required special access to the web server presenting the page.

Applets require no special access and can be written by programmers of all skill levels. You'll write several during the span of these 24 hours. You can test them with any web browser that handles Java programs. Most of the Java programs 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 Web.

By the Way

Because the applets you'll write in this book use Java 2, these applets must be run with a browser that supports the most current version of the language. During Hour 17, "Creating Interactive Web Programs," you'll learn how to set up a browser to use the Java plug-in to run Java 2 applets. All applets can also be tested with the appletviewer tool that is included with the Java Development Kit.


Unlike applications, applets do not have a main() block. Instead, they have several different sections that are handled depending on what is happening in the applet, as detailed fully during Hour 17. Two of the sections are the init() block statement and the paint() block. init() is short for initialization, and it is used to take care of 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 and save it when you're done.

Listing 4.3. The Full Text of RootApplet.java

 1: import java.awt.*;

 2:

 3: public class RootApplet extends javax.swing.JApplet {

 4:     int number;

 5:

 6:     public void init() {

 7:         number = 225;

 8:     }

 9:

10:     public void paint(Graphics screen) {

11:        Graphics2D screen2D = (Graphics2D) screen;

12:        screen2D.drawString("The square root of " +

13:            number +

14:            " is " +

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

16:     }

17: }


Compile this file using your Java development software. If you are using the javac compiler tool in the JDK, type the following at a command line:


javac RootApplet.java


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

By the Way

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 a Java interpreter. 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 2 applets by using Sun's Java plug-in, which requires special configuration of the web page.

  • Use the appletviewer tool that comes with the Java Development 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 17.

To see this applet using the appletviewer tool included with Java Development Kit, type the following at a command line:


appletviewer RootApplet.html


To see it using your computer's default web browser, type the following command instead:


RootApplet.html


You also can load this page with your browser: Choose File, Open, then navigate to the folder that contains RootApplet.html and select the file.

Figure 4.1 shows what the applet looks like when loaded with Mozilla Firefox.

Figure 4.1. The RootApplet applet displayed with a web browser.


Watch Out!

If you try to load this page with your web browser and get an error, the most likely cause is that your browser is not yet equipped with the Java plug-in. To correct this, visit the web page http://java.com to download and install the free Java Runtime Environment, which includes the Java plug-in.


    Previous Section  < Day Day Up >  Next Section