HelloWorld as an Applet

   

One of the features of Java that first drew public attention was its capability to add dynamic content to HTML pages using applets. Applets are Java classes that you can execute in a Java-enabled browser such as Netscape Navigator or Internet Explorer. Before applets were introduced, Web sites consisted solely of static HTML content.

Several differences exist between applets and applications. Most of these differences will be explained in Part II, "User Interface." For the HelloWorld example, the most important difference in the source code is that applets must define a subclass that inherits from the SDK's java.applet.Applet class (or its javax.swing.JApplet subclass if you're developing a Swing applet). For now, it's enough to say that you have to extend Applet for a class to be executed in a browser instead of as a standalone application.

See "Applets Versus Applications,"

Modifying and Compiling the Source Code

One of the simplest applets is the HelloWorld applet, the source code for which is shown in Listing 2.6. Right away, you should see that the applet HelloWorld is quite different from the HelloWorld application in Listing 2.1. You'll get more detail about the meaning of the source code a little later in this chapter. For now, copy Listing 2.6 into a file called HelloApplet.java and compile it.

Listing 2.6 HelloWorld as an Applet
 import java.applet.Applet; import java.awt.Graphics; public class HelloApplet extends Applet {   public void paint (Graphics g) {      g.drawString ("Hello World!",0,50);   } } 

Creating an HTML File

When you created the HelloWorld application in Listings 2.1 through 2.5, you ran them using the Java interpreter. Applets, however, don't run from the command line; they are executed within a browser. So, how do you tell the browser to open the applet?

If you have ever created a Web page, you are familiar with HTML. HTML uses a set of tags to define Web content that can be displayed by a browser. Just as you use the <TABLE> tag to insert a table into an HTML page, you use the <APPLET> tag to instruct a browser to load and execute an applet you have created.

The simplest HTML file for the HelloApplet class is shown in Listing 2.7. Although most browsers are forgiving when certain closing tags are omitted in an HTML file, don't forget to include the closing </APPLET> tag, or your applet might not be loaded correctly. Copy this text into a file called HelloApplet.html.

See "Applets,"

Note

With Java files, the filename must be the same as the class name . There is usually only one class per source file. This is not necessary with the HTML file. In fact, a single HTML file can contain several <APPLET> tags.


Listing 2.7 HelloApplet.html ” HTML File That Loads an Applet
 <HTML> <BODY> <APPLET CODE="HelloApplet.class" WIDTH = 200 HEIGHT=200> </APPLET> </BODY> </HTML> 

Running the Program in AppletViewer

The simplest way to run an applet is to use the AppletViewer program located in the bin directory of your SDK installation. AppletViewer is a scaled-down browser that looks for <APPLET> tags in a specified HTML file and opens a new window for each of them.

Note

If you are using Sun's SDK 1.3, the directory that holds the AppletViewer program is typically c:\jdk1.3\bin. Other development environments place this executable in other locations. You can always locate the program by searching for appletviewer.exe.


See "AppletViewer,"

To run HelloApplet using AppletViewer, type appletviewer HelloApplet.html on the command line. AppletViewer produces output such as that shown in Figure 2.1.

Figure 2.1. AppletViewer opens a new window and runs HelloApplet in it.

graphics/02fig01.gif

Running HelloWorld in Internet Explorer

AppletViewer provides a simple test environment for your applets, but you really should execute the applet inside a full-featured browser that recognizes the <APPLET> tag, such as Microsoft's Internet Explorer. To open HelloApplet in Internet Explorer, choose File, Open, and then select the HelloApplet.html file, as shown in Figure 2.2.

Figure 2.2. HelloApplet can also be run using Internet Explorer.

graphics/02fig02.gif

Now that you have seen how to run HelloApplet, you can see how the program works in the following sections.

Importing Other Classes

Notice the first two lines of the HelloApplet.java file:

 import java.applet.Applet; import java.awt.Graphics; 

The import statement is a new one. It enables you to use other classes. If you are familiar with the C/C++ #include directive, the import statement is similar (see "Packages" in Chapter 7).

See "Packages,"

HelloApplet uses two additional classes. The first is the java.applet.Applet class, which, as was stated before, is the parent of every class that runs in a browser as an applet. This class provides the basic functionality required for an applet to interface with the browser. Reusing the methods implemented by this class frees you from having to work with the details of how a browser loads and displays an applet.

The second class that is imported into HelloApplet is the java.awt.Graphics class, which provides the capability to draw to the screen or any other output device. A Graphics object lets you draw primitive shapes (lines, rectangles, and so on), display text, fill areas, specify clipping regions , select colors, and select fonts. In the case of an applet, its appearance is defined by method calls made on a Graphics object when the applet is loaded.

Declaring an Applet Class

The main difference, other than the class name, between the class declarations for HelloApplet and HelloWorld is that HelloApplet extends Applet. Remember in the last chapter how you learned about building a class structure? extends is the keyword that indicates that a class inherits from another class in a class hierarchy.

See "Extending Objects Through Inheritance,"

Because you imported the Applet class, you can refer to it as Applet in the extends clause. If you had not imported java.applet.Applet, you could still have extended it using its full name:

 public class HelloApplet extends java.applet.Applet { 

Applet paint Method

Perhaps the biggest difference between the implementations of HelloApplet and HelloWorld is that HelloApplet doesn't have a main method. Instead, this class only has a paint method. How is this possible given that the main method is the starting point when a class is loaded to start an application?

The answer lies in the fact that applets don't run in a standalone fashion like Java applications do. They are added to an already running program (the browser). The browser starts an applet and controls its execution by calling the methods defined in java.awt.Applet. One of these methods is paint:

 public void paint (Graphics g) { 

The browser calls the paint method any time the applet needs to be displayed on the screen. The paint method accepts a Graphics object that you can use to draw anything you want onto the screen space allocated to the applet. HelloApplet 's paint method makes a call to the drawString method of Graphics to display "Hello World!" within the browser window.

 g.drawString ("Hello World!",0,50); 

The Brief Life of an Applet

The paint method is not the only applet method that the browser calls, but the default implementations of those methods in java.awt.Applet work in the example without being overridden.

When the applet is loaded, the browser calls the init method to perform any work that needs to be done before the applet is displayed the first time. This method is only called once no matter how many times you return to the same Web page within a browser session. After init, the start method is called and then paint.

The start method is called every time an applet's parent Web page is accessed. This means that if you leave a page and then click the browser's Back button, the start method is called again. The stop method is called when you leave the applet's page, and destroy is called when the browser exits altogether.

Note

Notice that unlike the paint(Graphics g) method, the init(), start(), stop(), and destroy() methods do not take any parameters.


Let's look at how far you've come. You have now learned how to write a simple Java application and a corresponding applet. You have written to the screen and read from the keyboard using some of the core features of the language. You have also seen some of the exception handling capabilities that allow you to intercept and gracefully handle problems that might occur during execution of your programs. This chapter concludes with the introduction of two topics that will help you build on this knowledge to write more complex and useful programs.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net