Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Extending Applet

The most powerful statement in HelloApplet is in the class definition HelloApplet extends Applet. With that one statement, we’ve said that our little class, HelloApplet, inherits all the methods and variables of Sun’s much larger class, Applet.Applet already knows how to communicate with the Web browser. It knows how to communicate with the graphical interface inside the browser window. It even knows how to redraw its content (although that content is empty). What it doesn’t know is how to do any work. By extending Applet, we get a complete applet—ready to go. All we have to do is add content.

What’s a public Class?

Java provides several levels of access—public, protected, private, and a default level. Anyone who wants can access classes and members that are public. When you write an applet, you need to declare the applet’s class as public so that the Java environment can find it and run its methods.

Using Applet’s paint() Method

An applet draws into a graphical space provided by the Web browser. Whenever that space is covered (by another window) or disappears (because the user has minimized the window) it becomes invalid. When it becomes visible again, the applet must repaint itself. In fact, internally, Applet calls a method called repaint(). That repaint() method, in turn, calls paint(). In order to draw something into the applet’s graphical space, we need to do the work in paint().

The graphical space allocated for us by the browser is represented by a Graphics object, so we begin the paint() method by accepting that Graphics object (which we call theGraphics).

The Graphics class supports over three dozen public methods, including drawLine(), draw3DRect(), and fillArc(). Because we want to put a String into the graphic, the method we’re interested in is drawString().drawString() takes three parameters—the String we want to draw, and the x and y coordinates where we want to start drawing. We’ve chosen the coordinates 0 and 50 to start the String against the left margin, down a bit from the top.


NOTE:  The paint() method is one of several methods that the browser calls. You can get sophisticated behavior from your applet by overriding other methods, such as start() and init(). See the comments on “Life Cycle of an Applet” in the next section.

Life Cycle of an Applet

We’ve seen Applet’s paint() method. As you might guess, repainting the screen is only one part of an applet’s life cycle. This section shows the various stages of an applet’s life and suggests various tasks you may want to undertake at each step. If you’d like to see these steps in action, compile LifeCycle.java, given in Listing 37.4. You can construct an HTML file based on the pattern given in HelloApplet.html—just change the name of the class file.

Listing 37.4 LifeCycle.java—This Applet Demonstrates the Stages in the Life of an Applet


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

 public class LifeCycle extends Applet{
   public LifeCycle()
   {
     System.out.println(“Constructor running...”);
   }
   public void init()
   {
     System.out.println(“This is init.”);
   }
   public void start()
   {
     System.out.println(“Applet started.”);
   }
   public void paint(Graphics theGraphics)
   {
     theGraphics.drawString(“Hello, World!”, 0, 50);
     System.out.println(“Applet just painted.”);
   }
   public void stop()
   {
     System.out.println(“Applet stopped.”);
   }
   public void destroy()
   {
     System.out.println(“Applet destroyed.”);
   }
 }

If you run the LifeCycle applet from the appletviewer, you’ll receive standard out messages in the command prompt window. If you’re using a Web browser, you can open the Java console. To open the Java console in Netscape Communicator 4.0, for example, choose Communicator, Java Console. Figure 37.8 shows the console in action.


You can type commands into Navigator’s Java console. Enter a question mark (?) to see a list of commands. If you enter nine (9) you’ll put the console in maximum debugging mode—it will show you all kinds of information about your running applet.

Constructor

Every class has a constructor—you’ll spot it because it has the same name as the class. You can put initialization code in the constructor. Restrict yourself to code that should be run only once during the life of the applet.


FIGURE 37.8  You can write to the Java console to help debug your applets.


CAUTION:  

Not all browsers load and unload applets in the same way. For best results, put once-only code into init() rather than the constructor.


init()

When the browser sees an <APPLET> tag, it instructs the Java class loader to load the specified class. The Java environment in the browser makes an instance of the class (by calling its constructor). It then calls the instance’s init() method. The init() method is the best place to put code that should run only once during your applet’s lifetime. Experiment with the LifeCycle applet in different browsers to see the circumstances under which the constructor and init() are called.

start() and stop()

After your applet is loaded and initialized, the Java environment calls start(). If the user leaves the page or minimizes it, the applet’s stop() method is called. The start() method will be called again when the user returns to the page.

If your applet should take special action when the user enters or leaves the page, place the code for those actions in start() or stop().


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.