Lecture 3

  1. Review of what we've seen so far -- in class and in lab
  2. Using names: class exercise
  3. Working with the location of the mouse
  4. Using formal parameters and instance variables together
  5. Accessor methods

Review of what we've seen so far -- in class and in lab

WindowController organization

The programs we write for the first part of this semester will be programs that respond to user actions involving the mouse.

The code specifying the behavior of the program in response to each of those actions goes in one of the following methods:

   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)

Stuff to do at the beginning of program execution goes in the method:

   public void begin()

Graphical objects

We learned that some of the things we can do in our programs include creating graphical objects such as rectangles, ovals, lines, and text. We also learned that we can manipulate these objects in various ways. We can move them, color them, hide them, etc. We do any of these by sending an appropriate message to the object to be manipulated.

Naming

Of course, we can't send a message to an object unless we give it a name first.

In order to use a name, we first have to declare it. That is, we need to tell Java the name we'll be using and the type of object to which it will refer. For example,

   private FilledRect myRect;
   private FramedOval redOval, blackOval;
Declarations are formed by placing the name or names to be introduced after the name of the type of object with which the new name(s) will be associated.

Declarations like this enable Java to help you by detecting some of your typos.

Note that

Once you've declared a name, you need to associate a value with it:
   myRect = new FilledRect(10,10,30,20,canvas);
This is done using an instruction called an assignment statement. It consists of Finally, you can send a message to the object that you've named:
    myRect.hide();
Alternatively, we can say that we've invoked a method on the object.

Using names: class exercise

Say we want to write a program that will do the following: When it starts, it displays two words on the screen: "up" and "down". The word "up" is written in red and the word "down" is gray. If the mouse is pressed, "down" becomes red and "up" becomes gray; if the mouse is released, the word "up" will be red and the word "down" will be gray.

Demo. MouseIndicator


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.

Demo. Improved MakeBox

In the original program, the box was drawn by a construction like:

   new FilledRect(40, 40, 50, 50, canvas);
In the new program, this is replaced by:
   new FilledRect(point, 50, 50, canvas);
The name "point" comes from the method header. The text "(Location point)" in the header of each mouse event-handling method tells Java that we want to be able to use the name point to refer to the place where the mouse is located.

In our event-handling methods we don't simply have the option of including (Location point) in the header. We're required to do so.

Each time the mouse is pressed, before folowing the instructions in our method body, Java makes the name "point" refer to the coordinates of the current mouse position. When it sees the name "point" in the construction of the FilledRect, it uses the coordinates of the mouse as if we had typed them in while writing the program.

When used in this way, the name "point" is called a formal parameter. Note that the phrase "Location point" looks a lot like a variable declaration. The name Location describes the kind of thing that "point" will refer to.

There is nothing special about the name "point" in this situation other than that it appears in the method's header. Just as we can choose any word we want to use for an instance variable name, we can choose something other than "point" as a formal parameter name. For instance, we could take our ImprovedMakeBox program and change all "point"s to "mouseLocation" and the program would still work the same way.


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!

Demo. 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:
  1. The phrase on the right hand side of the equal sign in an assignment statement need not be a construction.
  2. A variable or formal parameter name can be associated with different things at different times in a program. Each time we drag the mouse, the meaning of the variable in this program changes.

Accessor methods

In playing with the "NotPhotoShop" application in lab, you saw that you could manipulate objects by changing their color, their location, their size, etc. You were also able to get at information about those objects.

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.

Demo. MouseMeter.