Lecture 3 (9/3/02)

Topics:

Simple event handling

Want a user to be able to interact with our program - a drawing that just sits there is no fun!

   

   protected void begin() //specify what happens when program first starts up.

   public void onMouseClick(Location point)

   public void onMouseEnter(Location point)

   public void onMouseExit(Location point)

   public void onMousePress(Location point)

   public void onMouseRelease(Location point)

   public void onMouseMove(Location point)

   public void onMouseDrag(Location point)

Demo1. MakeBox

Working with the location of the mouse

There's more to the mouse than simply its movements. We might actually care about its location. Fortunately, we can get at this information and use it. This is tied to the topic of "naming" in that this is another situation in which names are used to refer to information your program needs to work with.

As a simple example, consider the following program that is an improvement on our earlier MakeBox program. This one draws a box, but it does so at the current location of the mouse, rather than at a fixed location.

Demo2. Improved MakeBox

A formal parameter "point" which is a reference to a "Location" object is available "onMousePress"

public void onMousePress(Location point){

} // end onMousePress

Demo3. MouseIndicator

Declaration of a name

   FilledRect myRect;

   FramedOval redOval, blackOval;

Associating a value with that name:

   myRect = new FilledRect(10,10,30,20,canvas);

Using formal parameters and instance variables together

Consider a program that lets you draw (scribble?) by moving the mouse with the button depressed. Try it out!

Demo4. Scribble

How is this done? You can think of the scribble as a connected series of tiny line segments. Each time a mouse drag is detected, a tiny line is drawn from the last location it was pressed to the new location where it ends up.

Fortunately, the formal parameter "point" tells us the current location of the mouse when a dragging event occurs -- but this just gives us the current point. How do we remember where we last came from? And how do we remember where we currently are so that, as we move, we can connect the next line segment from the current point? The answer is that we can remember the current location in an instance variable.

There are a couple of interesting facts about variables and assignments illustrated by this example:

Accessor and Mutator methods

Objects consist of data (instance variables) and associated methods. Part of the reason for object-oriented programming languages is to restrict access to an object's data (called data encapsulation) to ensure that the object maintains a valid state.

If we want the user of an object to view part of an object's data/state, then we provide an accessor method. For example, we can find out the width and color of a FilledRect myBox by calling the accessor methods myBox.getWidth() or myBox.getColor().

If we want the user of an object to be able to change part of an object's state, then we provide a mutator method. For example, we can change the width and color of a FilledRect myBox by calling the mutator methods myBox.setWidth(300) or myBox.setColor(Color.red).

You've now also seen that you can use the information associated with the "point" parameter as if it were a pair of x and y coordinates. So you might expect that there would be some way to get at the indivdual coordinates if you needed to do so.

This can be done using a Java mechanism called an accessor method. Recall that we said earlier that you can manipulate an object by sending it a message or by invoking a method. The methods invoked in those cases were called mutator methods since they affected the objects. Accessor methods, in contrast, provide us with information about an object.

To use an accessor method, you use syntax very similar to that for a mutator method. To get the x coordinate associated with "point" you would write

point.getX()

and to get at the y coordinate you would write

point.getY()

The way you use them, however, is quite different from the way you would use a mutator. Trying to use

point.getX();

as a command on its own makes no sense. It only makes sense to get the information about the x coordinate if you plan to do something with it. Consider the following example that tracks the movements of the mouse as it's moved and displays its coordinates on the canvas.

Demo5. MouseMeter.