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.
Of course, doing something with the information about the x and y coordinates of a Location isn't limited to displaying that information. This is illustrated by the following example that tracks the movements of the mouse visually.
Demo. CrossHairs.
"This is a string."There are many contexts in which strings are useful. We'll spend a fair bit of time learning more about them during the second half of the semester. For now, it will be sufficient for us to discuss how strings can be put together in order to create interesting and useful output.
In the MouseMeter example above, we wanted to display the x and y coordinates of the mouse, and we wanted the displayed information look like a parenthesized coordinate pair. To do so, we needed to create a string that was made up of the x and y values, as well as the textual left parentheses.
We can concatenate two string using the operator +. Thus
"This is" + "a string."gives us the string above:
"This is a string."We can also convert numbers to strings by concatenating them with strings. Therefore, if the mouse is at Location (10,20), then
"Mouse x coordinate is " + point.getX()gives us the string:
"Mouse x coordinate is 10"So to create a string like (x, y) from the mouse's x and y coordinates, we had to say:
"(" + point.getX() + "," + point.getY() + ")"
new Number(7)or
7.addYourself(...)It's possible, but Java treats basic numeric types differently.
One numeric base type is int, representing integers, with operations +, -, *, /, and % (which yields the remainder after division). Integer division is interesting. Any remainder is truncated. Thus 14 / 4 yields 3. (14 % 4 yields 2).
Here's an example that makes use of ints:
Demo. Click Counter
And here's a familiar program that can be written better with the use of ints:
We call this a constant, and Java gives us a special way to designate it as such. We can declare a name to be a constant by writing it in the form:
private static final int circleInset = 7;The most important word here is "final". This means that its value cannot be changed. This is the essence of a constant.
There are a few advantages to designating constants:
Demo. Prettier sign program
Not everything can be a constant. Constants can't depend on anything created when the program starts up (except other constants). In particular, a constant may not depend on canvas. Thus we may never have a constant of type FilledRect, FramedOval, etc.
Of course, there's more to it than this. We only want to move the box if the user actually pressed the mouse on the box. We need to be able to detect that, and we need to make sure that we only drag the box if the mouse was on the box.
Luckily, we have a method for graphic objects which allows us to detect whether a point is in the object.
item.contains(point)will have value true if point is in the bounding box of item, and has value false otherwise.
In order to write this program, we want to write something like:
if item.contains(point) then move the box the appropriate amountLet's see how we do this in Java.
Demo.MouseDrag
Notice that if we use contains with an oval, it will only tell us if the point is inside the bounding rectangle, not within the oval!