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()
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
myRect = new FilledRect(10,10,30,20,canvas);This is done using an instruction called an assignment statement. It consists of
myRect.hide();Alternatively, we can say that we've invoked a method on the object.
Demo. MouseIndicator
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.
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.
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.