sepbar-1.gif (3106 bytes)

Java 106 : Introduction to applets

Written by David Reilly, August 24, 1997

sepbar-1.gif (3106 bytes)

Previous | Next

In previous tutorials, we've been dealing with classes that contain a main method, and run as standalone applications. Java code can also be executed under the control of a web browser. Code written to execute under the control of a browser is called an applet. Mini-applications, these applets have access to a subset of the normal Java network and file I/O access. Applets can be used to provide dynamic user-interfaces, and a variety of graphical effects for web pages.

To create an applet, we extend the java.applet.Applet class (readers who are unfamiliar with class inheritance should consult the previous tutorial Java 104 : Extending classes). By overriding the methods of java.awt.Applet, we can provide new functionality, and create useful programs which can be placed into web pages.

Our first applet

For our first applet, we'll write a "Hello world" application. The first tutorial showed how to write a stand-alone application that displayed the message "Hello world" to the user's screen. Writing the same program as an applet takes no more effort, and it can then be placed on any webpage.

We start by extending java.applet.Applet, and importing the applet and awt package. The AWT package represents the Abstract Windowing Toolkit, a library of classes that give applets and stand-alone applications graphical capability.

To display the greeting, we must override the paint method of java.awt.Applet. The paint method deals with the display of graphics on our applet - we'll override it to show our message. We set the colour to blue, and then draw a string on our applet window.



/*
 *
 * HelloWorldApplet.java
 * Demonstration for Java 106 tutorial
 * David Reilly, August 24, 1997
 *
 */

import java.awt.*;
import java.applet.*;

class HelloWorldApplet extends Applet
{
        // Default constructor
        public void HelloWorld()
        {
                // Call parent constructor
                super();
        }

        Overridden paint method
        public void paint ( Graphics g )
        {
                g.setBackground ( Color.white );
                g.setColor   ( Color.blue );
                g.drawString ( "Hello world!", 0, size().height - 5);
        }
}

Listing 1.0 - HelloWorldApplet.java


The paint method takes as a parameter a graphics object. In the paint method, we set our background to white, and the drawing colour to blue, by executing methods of the graphics object. Finally, we draw a string on the applet, at offset 0, size().height -5.

In applets, our coordinate system is based on the fact that 0,0 represents the top left hand corner of the applet. By drawing the message at 0, size().height -5, we begin our text at the left, and near the bottom of the applet.

Once you've compiled the HelloWorldApplet class, you need to insert it into a webpage for testing. We use the <applet> tag for this.

<applet code="HelloWorldApplet.class" width=150 height=30>

This tag loads the applet HelloWorldApplet.class, giving it a width of one hundred and fifty pixels, and a height of thirty. When the class is copied to the same directory as the HTML document it is to be inserted into, the applet will load whenever a Java enabled browser views the page. You can see the result at the below, where the HelloWorldApplet has been inserted into this document.

Applet not displayed - requires a Java enabled browser

For those who would prefer not to have to type (or copy from the clipboard) the code for this tutorial, I've put together the full source code and applet for this tutorial together as a ZIP.

By now, you should have a basic understanding of what an applet is, and how to place one inside a HTML document. In the next tutorial, we'll cover more advanced applet topics, such as the applet life-cycle, GUI event handling, and the AWT package. 

Previous | Next

<-- Back to the Java Coffee Break