Lecture 2

  1. Announcements
  2. Introduction to Java programming
  3. Fun with graphics
  4. The canvas
  5. Graphics primitives
  6. Magic words
  7. Simple event handling
  8. Naming
  9. Working with the location of the mouse

Announcements

None today.

Introduction to Java programming

Many of our first programs will simply respond to high-level commands such as a press of a mouse button or a mouse movement. If we want a program to react to the pressing of the mouse, we have to tell it what to do when we press the mouse.

Consider, for example, the following simple program that draws a box on the screen when we click the mouse.

Demo 1. MakeBox


Fun with graphics

This semester we'll be working with graphics a great deal. It's natural to want to work with graphic objects like shapes, lines, text, pictures, etc. They're fun.

In addition to being fun because they involve graphics, our programs will be fun because they're applets. That is, they're the kind of Java program that can be embedded in a web page.


The canvas

When you write a program in this class, you should think of it as an object that responds to actions like mouse presses, movements, etc. You'll specify what the program should do by giving it a list of commands to follow in response to each of those actions.

Many of those commands will involve things like placing graphical objects in a window (on the screen). You should think of that window as a canvas. In fact, we'll call it a canvas. In order to place something on the canvas, you'll naturally need to tell the program where to put it. To do so, all you have to do is specify the location in terms of x and y coordinates. The only trick is that the coordinate system is upside down. The upper left corner of the canvas is position (0,0). As you move to the right, you get to higher and higher x coordinates. As you move down, you go to higher and higher y coordinates. (Normally, moving down from the origin would get you into the range of negative y values!)


Graphics primitives

The basic objects you can draw are: and this is how you'll build these:
   new FramedRect(10, 10, 40, 60, canvas);
   new Line(x1, y1, x2, y2, canvas);
   new Text("hello there", x, y, canvas);
That is, any time you want to create a new framed rectangle, you'll specify that you want a new FramedRect, with its upper left corner at the x coordinate 10, the y coordinate 10, with a width of 40, and a height of 60. You also have to say where you'd like it placed -- on the canvas, naturally.

In the case of a line, you'll specify the x and y coordinates of each endpoint.

For text you'll give the actual text you'd like displayed. You'll need to put quotes around the text, but the quotes won't actually appear on the canvas. Now let's look carefully at a program that does nothing but display a box on the screen.

Demo 2. Simplest Box


Magic words

You should have noticed the line "new FilledRect(...." in the program that dispayed a box. But you certainly also noticed that there were many other lines in the program as well. It might look like there are a large number of magic words required to make this program acceptable to the computer, but the overall structure is really quite simple.

Simple event handling

We said earlier that a program will be an object that responds to actions such as the pressing of the mouse. A program like the one we just looked at -- that draws a box and does nothing more -- really isn't much fun at all.

There are seven mouse actions with which you can asociate a method within a program that extends WindowController. They are:

   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)
If you look back to the program for our first demo, you'll see that it contains a method "onMousePress". In it we say that on a mouse press, the detail that the program should carry out is the drawing of a box.

Naming

OK, now say we want to write a program that will do the following: if the mouse is pressed, it will print the word "down" in red and the word "up" in gray; if the mouse is released, the word "up" will be red and the word "down" will be gray.

One command we need to know is that to set something's color we say:

   setColor(Color.xxx);
but whose color do we set? How do we specify which text is to be red and which is to be gray? We need a way of identifying each of them. This involves three steps:
  1. Declaration of a name.
  2. Associating a value with the name.

Demo 3. MouseIndicator

Declaration of a name

   private FilledRect myRect;
   private FramedOval redOval, blackOval;
Associating a value with that name:
   myRect = new FilledRect(10,10,30,20,canvas);

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.

Demo 4. Improved MakeBox