Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Applications, applets, and hybrids

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

  • Print
  • Feedback

Page 4 of 5

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 Lifecycle.java). 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!">
</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.

  • Print
  • Feedback

Resources