Applications, applets, and hybrids

Java 101 charts a new course, and explores applications, applets, and hybrids

1 2 3 Page 2
Page 2 of 3

The browser calls init() method after creating a class_name object (a task that occurs behind the scenes). Generally, you would perform one-time initialization, such as creating the program's GUI, in that method. init() features the following signature: public void init ().

The start() method is called immediately after init() and whenever the Webpage containing the applet is revisited. That method represents a good location to begin a background thread for performing animation (which we'll investigate in a future article). It has the following signature: public void start ().

The browser calls the stop() method prior to leaving a Webpage and just before calling the destroy() method. You would normally place code that terminates a background thread in stop(). It has the following signature: public void stop ().

Finally, the browser calls destroy() before exiting or replacing a stopped applet in its cache. That method presents an appropriate spot for canceling any global initialization such as closing a file that was opened in init() (unrestricted applets only). destroy() has the following signature: public void destroy ().

To give you an idea of what a simple applet looks like, I've written a small applet called Lifecycle. The source code is presented in Listing 3.

Listing 3.

// To run applet under Netscape Communicator 4.7 browser or Internet
// Explorer 5.0 internal JVM, compile using javac -target 1.1
import java.awt.*;
public class Lifecycle extends java.applet.Applet
   String msg = "Hello World";
   public void init ()
      System.out.println ("init called");
      String s = getParameter ("message");
      if (s != null)
          msg = s;
   public void start ()
      System.out.println ("start called");
   public void paint (Graphics g)
      g.drawString (msg, 10, 30);
   public void stop ()
      System.out.println ("stop called");
   public void destroy ()
      System.out.println ("destroy called");

When the browser calls either init(), start(), stop(), or destroy(), those methods in turn call System.out.println to send a message to the standard output device.

I've declared a String variable, called msg, and initialized that variable to Hello World (all the characters between the double quote characters). Behind the scenes, an object of type String is created, and the characters between the double quotes are copied into that object. (You'll learn more about strings in a future article.)

The init() method calls the getParameter() method (inherited from Applet) to retrieve the value of an applet parameter. An external name/value pair, an applet parameter dynamically configures an applet without recompilation. The name of the parameter is identified as message -- the sole String argument to getParameter(). If message exists, getParameter() returns its value. If it doesn't exist, a special null value returns. Java supplies the null reserved word to indicate a null reference -- similar to a zero pointer in C or C++. We'll look more closely at null in next month's article.

After calling init(), the browser calls the paint() method to tell the applet to draw something in its rectangular area. The address of a graphics context -- an object that represents a video screen or printer -- is passed to paint(), and the applet calls various graphics context methods, such as drawString(), to perform drawing. The drawString() method takes three arguments -- a String object that specifies the characters to be drawn, an x coordinate (10) that indicates the number of pixels to the right of the rectangular area's left edge, and a y coordinate (30) that distinguishes the number of pixels below the rectangular area's top edge. The (x, y) coordinates -- (10, 30) in the program -- identify the location of the left-most character in the rectangular area.

By now, you're probably itching to try out Lifecycle. Before you can do that, you'll need to compile the source code (as in javac Then, you'll need some HTML code to present to the browser. Listing 4 provides that code.

Listing 4. Lifecycle.html

<applet code="Lifecycle.class" width=250 height=50>
  <param name="message" value="What an applet!">

To run an applet under the control of the browser's virtual machine, you use an HTML tag called <applet>. That tag requires you to specify the name of the class file, whose class extends Applet (by way of the code attribute). Furthermore, you indicate the width and height of the applet's rectangular area by stating the width and height attributes. In this case, Lifecycle.class constitutes the applet, and the dimensions of the rectangular area equal 250 by 50 pixels.

Each <applet> tag must have a matching </applet> closing tag. As you can see, another tag <param>, which describes an applet parameter, can sandwich between those tags. The value of the <param> tag's name attribute matches the argument passed to the getParameter() method. In both cases, message is specified. getParameter() returns the value of the <param> tag's value attribute. In this case, What an applet! is identified. By using <param> tags, you save yourself the trouble of recompiling your applet to change hard-coded items (such as image filenames and sound filenames).

When it comes time to run the applet, you can either fire up your browser and specify the location and name of Lifecycle.html in the browser's location text field, or you can use the appletviewer program that comes with the Java 2 SDK.

The appletviewer program takes a single argument -- the applet's HTML file. To run Lifecycle, type appletviewer Lifecycle.html. After a few moments, a window should appear with the required output. The first time you run appletviewer, you'll probably receive a welcome screen. Select Ok (or whatever button is presented), and you'll see your applet. That welcome screen will only appear during appletviewer's first run.

I could go on and on about applets, but I don't want to overwhelm you. I will discuss additional features in future articles. Right now, let's take a look at the third category of Java programs -- hybrids.


A hybrid combines application and applet characteristics, and can run as either a standalone application or a browser-dependent applet. In application mode, support code is provided to mimic a browser environment. That support code creates a GUI window for the applet, creates a stub, and creates a context. Our own appletviewer program will result. (For those who want to learn more about stubs and context, I recommend checking out the Java 2 SDK documentation on the AppletStub and AppletContext interfaces, and the Applet class.)

Listing 5 presents source code to a hybrid program called Hybrid. Don't be alarmed by all the source code. Our objective is to compile that code and run it as either an application or an applet. The code will make much more sense after you've progressed through this column.

Listing 5.

1 2 3 Page 2
Page 2 of 3