Applets

Tom Kelliher, CS 116

Sept. 4, 2002

Administrivia

Announcements

Assignment

From Last Time

Lab 1.

Outline

  1. A simple applet.

  2. Exercise with applets.

Coming Up

Applets and variables.

A Simple Applet

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

// The Hello class simply displays the string "Hello, world!"
// within the graphics object.

public class Hello extends Applet
{
   public void paint(Graphics g)
   {
      g.drawString("Hello, world!", 20, 10);
   }
}

  1. Importing classes that are already defined in Java.

    Classes must be imported before you can use them. We are importing the applet and awt packages.

  2. Defining our own class.

    We are defining our own applet which inherits everything from the Applet class:

    public class myClassName extends Applet
    {
       // ...
    }
    

  3. Defining our own method.

    Within our applet we define a method paint which is used to draw the applet on the screen. A method is defined as follows.

    visibility returnType methodName(formal argument list)
       {
          ...statements...
       }
    
    1. The visibility specifier can be either public or private to indicate whether method can be used outside the class or not.

    2. The returnType indicates what kind of value is produced by the method. The specifier void means that the method does not return a value.

    3. The formal arguments are input values to the method. (paint takes one argument, a Graphics object)

    4. The body of the paint method.

      The paint method changes the Graphics object g to specify how the Applet should be drawn.

Exercise with Applets



Thomas P. Kelliher
Thu Aug 29 17:03:02 EDT 2002
Tom Kelliher