2.20 The Applet Class Revisited2.19.3 Rules of Thumb2 Java with Caffeine3 Spilt Java

2.20 The Applet Class Revisited

We complete this section by revisiting the class Applet that we have used up to now to implement HTML-embedded programs with graphical output.

  public class Name extends Applet
  {
     public void paint(Graphics graph)
     {
       paint operations on graph
     }
  }

Any applet is a subclass of class Applet provided in package java.applet. This class is part of the following subclass hierarchy

  java.lang.Object
    |
    +--java.awt.Component
         |
         +--java.awt.Container
              |
              +--java.awt.Panel
                   |
                   +--java.applet.Applet

i.e., it itself inherits a lot of functionality from other classes. For instance, the class Component is implemented as

  public abstract class Component implements ...
  {
     ...
     public void update(Graphics g)
     {
       if (...)
       {
         g.setColor(getBackground());
         g.fillRect(0, 0, width, height);
         g.setColor(getForeground());
       }
       paint(g);
     }

     public void paint(Graphics g) { }
  }

The class is declared as abstract, i.e., it is not intended for instantiation. It provides an update method, which clears the applet window and then invokes the paint method to draw the window contents. The update method is invoked by the browser whenever the window content is to be redrawn.

The method paint itself is declared with an empty body, i.e., it is expected that a subclass should override it with a corresponding functionality (it would have been better style to declare this method as abstract). This is exactly what we do in our own applet code.


© Wolfgang Schreiner; February 3, 2005

2.20 The Applet Class Revisited2.19.3 Rules of Thumb2 Java with Caffeine3 Spilt Java