Lecture 4
Stuff to do at beginning of program execution goes in method:
public void begin()
Stuff to do in response to user actions goes in one of:
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)
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 from lecture 3, 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() + ")"
Can't send a message to an object unless you give it a name!
FilledRect myRect;
Associating a value with that name:
myRect = new FilledRect(10,10,30,20,canvas);
Sending a message or invoke a method or call a method:
myRect.moveTo(50,90);
We've been discussing graphical objects exclusively.
First base type is int, representing integers, with operations +, -, *, /, and % (representing remainder after division).
What do we do with integer division? Truncate any remainders. I.e., 14 / 4 = 3 and 14 % 4 = 2.
Here's an example that makes use of ints:
Demo. Click Counter
Recall the simple bridge program we wrote:
What if the numbers we're using will always be the same?
There are two kinds of named values: variables and constants. So far we have only used variables.
There are two advantages to using constants:
private static final int ROAD_HEIGHT = 30;
Let's take a moment to explain all the magic words here.
Private means that no class outside of the class being defined here gets to see this constant. Alternatives are public (everyone gets to see it) and protected. We'll explain protected in more detail later in the term. Right now only certain methods from WindowController are labelled with protected. It has to do with the ability to override behavior defined in the class you are extending.
static means that only one copy of the value is kept around (for efficiency). It's hard to imagine why we might want more than one copy of a given constant, so they are always declared static.
final means that its value cannot be changed. This is the essence of a constant.
The rest of the declaration just says that the constant is going to be of type int, has name ROAD_HEIGHT, and has the value 30. Because the values of constants can never be changed, they must be given an initial value in their declarations. We can also give initial values to names representing variables in their declarations, but we are not required to do so.
Having gone to all this effort to explain all the keywords in a constant definition, really all you need to do is always write down the magic incantation
private static final ...
at the head of all constant declarations. The following program is a revision of the two previous versions which uses constants:
Demo. Prettier bridge program
Not everything can be a constant. Constants can't depend on anything created when program starts up (except other constants). In particular, a constant may not depend on canvas. For example, all of our constructor routines for graphics primitives depend on canvas. Thus we may never have a constant of type FilledRect, FramedOval, etc.