Interface I
Home Up Interface I Interface II Interface III Interface IV Interface V Interface VI Interface VII Interface VIII Interface IX Interface X Interface XI Interface XII Code

 

There are two goals for today's lesson.  First, to understand the mechanics of a very simple applet.  Second, to understand the common supporting HTML tags that enable an applet to be viewed by a web browser.

A Simple Applet

import java.applet.*;
import java.awt.*;        
public class SimpleApplet extends Applet
{
  public void paint (Graphics g)
  {
    setBackground(Color.blue);
    g.drawString(getParameter("message"), 50, 100);
  }
}        

Code Explanation

There are two libraries that must be imported for the applet to work:   java.applet.* provides the Java program with the ability to be an applet and java.awt.* provides the program with the ability to do graphics.  AWT is short for "Abstract Windowing Toolkit".

Following the import statements, the first line of code states that the main class (SimpleApplet) is going to inherit methods from the Applet class.  This is done through the "extends" syntax.  The name of the file that this code is stored in must be called SimpleApplet.java since SimpleApplet is the name of the public class.

There are many methods that can appear within an applet.  Paint is the only one that is used for this simple example.  The paint method takes the graphics context as a parameter and then proceeds to do the following two things.  First, it sets the background color of the applet to blue through the built-in graphics method "setBackground" and the built-in graphics constant "Color.blue".   Then, it displays the value of the HTML parameter "message" in position 50, 100 within the applet drawing area.  The top, left coordinate of the drawing area is 0,0.  Thus, the coordinate (50,100) is 50 pixels to the right of the top, left corner and then 100 pixels down from the top row.   The "drawString" method is covered in more detail in the language reference summary regarding standard graphics.

The Supporting HTML File

<HTML>        
<TITLE>A Simple Applet</TITLE>        
<BODY>        
This applet displays a        
<APPLET CODE="SimpleApplet.class" 
        CODEBASE="." 
        WIDTH=200 HEIGHT=300
        ALT="If you had a Java enabled browser, you would
             see a blue rectangle here.">
<PARAM NAME=message VALUE="Simple Applet">
</APPLET>        
200 pixel wide by 300 pixel tall rectangle.        
</BODY>        
</HTML>        

HTML Explanation

HTML includes several tags that support applets.  Looking at the code above, which is located in a file called "SimpleApplet.html", we see the following APPLET tags:

CODE:  The code tag is required and indicates the name of the class file that is to be run.  Since the main class in the program is named "SimpleApplet", this is the class that must be indicated.
CODEBASE: This optional tag indicates the directory where the CODE class file resides.   If it is omitted, the class file is assumed to reside in the same directory as the html file.  In the example here, the "." is equivalent to the default (i.e. the class file resides in the same directory that the html file resides in).
WIDTH: The width in pixels for the applet.  In this case, the applet is 200 pixels wide.
HEIGHT: The height in pixels for the applet.  In this case, the applet is 300 pixels wide.
ALT: The message to be displayed, in the event that the html page is viewed from a browser that does not support Java.
PARAM: An optional method for allowing the html file to communicate with the Java applet.  Here there is a parameter whose name is "message" and whose value is "Simple Applet".  The applet above grabs the value of the parameter by invoking the method "getParameter("message")" and then prints it out using the "drawString" method.

In addition to the tags that appear in the file above, there are some other tags that are occasionally useful.  To learn more about these tags, you should consult a more complete reference or conduct experiments on your own.

ALIGN:  The value for this tag indicates how the applet drawing area is aligned with the text in the html file.  Values include TOP, MIDDLE, BOTTOM, RIGHT, and LEFT.
HSPACE, VSPACE:  The values for these tags indicate how many pixels should be left open to the left and the right (HSPACE) or the top and the bottom (VSPACE) of the applet.
ARCHIVE: This tag indicates the location of a Java Archive (JAR) file.
NAME: This tag allows two applets on the same html page to communicate with one another.
OBJECT: This tag indicates the name of a serialized applet object.

What You'll See: