Lecture 4

  1. Accessor methods
  2. Strings
  3. Numeric types
  4. Constants
  5. Conditionals

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.

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.


Strings

When we construct new Text, we specify the String to be displayed. A string is simply a sequence of characters. In Java we surround string literals by double quotes:
   "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() + ")"

Numeric types

We've discussed graphical objects almost exclusively to this point. You can What if we want to manipulate numbers? (other than to do the simple sort of display we saw above) Would you want to have to say
   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:


Constants

In the sign example above, we made use of many integer variables. In that example, though, it seems odd to call them variables. After all, their values don't change. circleInset, for example, is given the value 200 and it never changes at any other point in the program.

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:

  1. It signals to the reader of the program that the value won't change.
  2. It protects you from accidentally changing a value that you didn't mean to change.
  3. It allows for certain compiler optimizations.
Let's look at our sign program again. This time it uses constants, rather than variables.

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.


Conditionals

We start out by looking at a program which allows the user to drag a box around the screen. If the user presses the mouse on the box and then drags the mouse around then, the box should move just as much as the mouse does.

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 amount
Let'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!