Java Applet Tutorial, Java 1.1

Java Applet Tutorial, Java 1.1

Maryland Flag Stripe

Gregory C. Walsh, Department of Mechanical Engineering

Introduction
Description of Code
My Version of the applet
Remarks
Maryland Flag Stripe

Introduction

In this lesson we consider building simple applets using the Applet class and the Absract Windowing Toolkit. Our first program will be a simple hello world applet. Two files are needed, Lesson1.java and View.html, as indicated in the figure below. The java compiler, who command line is javac , produces a byte code file Lesson1.class, named after the class defined in Lesson1.java. The html fiel then tells your web browser or your appletviewer where to find the applet byte code, Lesson1.class. After the byte code file Lesson1.class is produced, it can be run type typing appletviewer View.html

Description of Code

We will examine the code line by line. The following code needs to be saved in a file called Lesson1.java.

// file:   Lesson1.java
// type:   Java source
// note:   A first applet in Java
// author: Gregory C. Walsh
// date:   3.9.1999

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

public class Lesson1 extends Applet {
  public void paint(Graphics g) {

    g.drawString("Your first Java Applet!", 20, 30);

  }
}

The first few lies are single line comments. The two import statements are similar to include statements in a C program - they allow us to draw upon the prewritten class Applet in java/applet and also every class in the directory java/awt/ . There is no preprocessor in Java, unlike C! We intend to use methods and classes defined in both directories.

The first line of the code defines a public class Lesson1. Note that this class etends Applet, so it inherits all of class Applets methods and data. This is good because making an applet is actually really complicated, and many functions and data re required. We don't really care about these functions for this applet, so inheriting them from a generic applet is fine. We only care about one function, the paint function.

The paint function of the Applet is called whenever the window needs to be drawn. The data of the picture and its associated functions are wrapped up together in an object of the type Graphics. The paint function is passed a reference to this object when it is called so it may access and change this data. We of course will use function provided by the Graphics object to figure out which pixels to set what color.

To compile this code, we need to first tap java if we are on a Unix box or open an Console window (MSDOS) on a PC and set up the PATH variable so the tools can be found (see jpath.bat on the turtorial page). To run this applet, we need an html file with the following like embedded in it:

< applet code="Lesson1.class" width=200 height=50 > </applet>

My version of the applet

Remarks

With a few lines of code, we have written a program to generate a window with some text appearing on the inside. We are able to do this by creating the class Lesson1 as an extension of the class Applet. Our class inherits all of the prewritten Applet methods and data. The key point is that we do not have to write that code ourself.

Code development in Java is inverted from the C code development. In C, we would collet the functions we neede from the Windows API (applications programmer's interface) and insert these into a framework (the function main, or under Windows, WinMain) we write. In Java, we instead find a framework (the Applet class) which does largely what we want and confine our development to the methods in the framework which differentiate our applet from the generic applet. We override functions like paint if we need the functionality to be other than the default. If the default is ok, we write nothing - for example, we did not override the init function.


Originally developed in 1999 by Greg Walsh
Modified in April 2001 by Mark Austin
Copyright © 1999-2001, Departments of Civil and Mechanical Engineering, University of Maryland