Previous Table of Contents Next


One of the major changes between Java 1.0 and Java 1.1 is in the way that Java programs are notified of “events,” such as mouse motion. Example 14.2 uses the Java 1.0 event model rather than the preferred Java 1.1 event model. This is because the current generation of Web browsers (as this is written) still use Java 1.0. In order for this applet to be widely usable, it is coded with the old, “deprecated” event model.3


3If you are interested in updating this program to use Java 1.1, you must learn how to use the new 1.1 event model. In addition, you need to change the call to bounds() in the action() method to a call to getBounds(), if you want to avoid a compilation warning about using a deprecated method.

EXAMPLE 14.2. A Java applet.

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

   public class Scribble extends Applet {
     private int last_x, last_y;
       // Store the last mouse position.
     private Color current_color = Color.black;
       // Store the current color.
     private Button clear_button;
       // The clear button.
     private Choice color_choices;
       // The color dropdown list.
       // This method is called to initialize the applet.
       // Applets don’t have a main() method.
     public void init() {
       // Set the background color
       this.setBackground(Color.white);

       // Create a button and add it to the applet.
       // Set the button’s colors
       clear_button = new Button(“Clear”);
       clear_button.setForeground(Color.black);
       clear_button.setBackground(Color.lightGray);
       this.add(clear_button);

       // Create a menu of colors and add it to the applet.
       // Also set the menu’s colors and add a label.
       color_choices = new Choice();
       color_choices.addItem(“black”);
       color_choices.addItem(“red”);
       color_choices.addItem(“yellow”);
       color_choices.addItem(“green”);
       color_choices.setForeground(Color.black);
       color_choices.setBackground(Color.lightGray);
       this.add(new Label(“Color: “));
       this.add(color_choices);
     }

       // This method is called when the user clicks
       // the mouse to start a scribble.
     public boolean mouseDown(Event e, int x, int y)
     {
       last_x = x; last_y = y;
       return true;
     }

     // This method is called when the user drags the mouse.
     public boolean mouseDrag(Event e, int x, int y)
     {
       Graphics g = this.getGraphics();
       g.setColor(current_color);
       g.drawLine(last_x, last_y, x, y);
       last_x = x;
       last_y = y;
       return true;
     }
       // This method is called when the user clicks
       // the button or chooses a color
     public boolean action(Event event, Object arg) {
       // If the Clear button was clicked on, handle it.
       if (event.target =\^= clear_button) {
         Graphics g = this.getGraphics();
         Rectangle r = this.bounds();
         g.setColor(this.getBackground());
         g.fillRect(r.x, r.y, r.width, r.height);
         return true;
       }
       // Otherwise if a color was chosen, handle that
       else if (event.target =\^= color_choices) {
         if (arg.equals(“black”)) current_color = Color.black;
         else if (arg.equals(“red”)) current_color = Color.red;
         else if (arg.equals(“yellow”)) current_color = Color.yellow;
         else if (arg.equals(“green”)) current_color = Color.green;
         return true;
       }
       // Otherwise, let the superclass handle it.
       else return super.action(event, arg);
     }
   }

Don’t expect to be able to understand the entire applet at this point. It is here to give you the flavor of the language. In the sections “How Java Differs from C” and “Classes and Objects in Java,” we’ll explain the language constructs you need to understand the example.

The first thing you should notice when browsing through the code is that it looks reassuringly like C and C++. The if and return statements are familiar. Assignment of values to variables uses the expected syntax. Procedures (called methods in Java) are recognizable as such.

The second thing to notice is the object-oriented nature of the code. As you can see at the top of the example, the program consists of the definition of a public class. The name of the class we are defining is Scribble; it is an extension, or subclass, of the Applet class. (The full name of the Applet class is java.applet.Applet. One of the import statements at the top of the example allows us to refer to Applet by this shorter name.)

Classes are said to “encapsulate” data and methods. As you can see, our Scribble class contains both variable and method declarations. The methods are actually defined inside of the class. The methods of a class are often invoked through an instance of the class. Thus you see lines like:

   color_choices.addItem(“black”);

This line of code invokes the addItem() method of the object referred to by the color_choices variable. If you’re a C programmer, but not a C++ programmer, this syntax may take a little getting used to. We’ll see lots more of it later. Note that this is a keyword, not a variable name. It refers to the current object; in this example, it refers to the Scribble object.

The init() method of an applet is called by the Web browser or applet viewer when it is starting the applet up. In our example, this method creates a Clear button and a menu of color choices, and then adds these GUI components to the applet.

The mouseDown() and mouseDrag() methods are called when the user clicks and drags the mouse. These are the methods that are responsible for drawing lines as the user scribbles. The action() method is invoked when the user clicks on the Clear button or selects a color from the menu of colors. The body of the method determines which of these two events has occurred and handles the event appropriately. Recall that these methods are part of the Java 1.0 event model.

To compile this example, you’d save it in a file named Scribble.java and use javac:

   % javac Scribble.java


Previous Table of Contents Next