Invoking Methods on a Graphics Object

In order to draw things on the screen, you invoke methods on the graphics object. We have invoked lots of methods already, but this is the first time we have "invoked a method on an object." The syntax is similar to invoking a method from another class:

g.setColor (Color.black);
g.drawOval (x, y, width, height);

The name of the object comes before the dot; the name of the method comes after, followed by the arguments for that method. In this case, the method takes a single argument, which is a color.

setColor changes the current color, in this case to black. Everything that gets drawn will be black, until we use setColor again.

Color.black is a special value provided by the Color class, just as Math.PI is a special value provided by the Math class. Color, you will be happy to hear, provides a palette of other colors, including:

To draw on the Slate, we can invoke draw methods on the Graphics object. For example:

g.drawOval (x, y, width, height);

drawOval takes four integers as arguments. These arguments specify a bounding box, which is the rectangle in which the oval will be drawn (as shown in the figure). The bounding box itself is not drawn; only the oval is. The bounding box is like a guideline. Bounding boxes are always oriented horizontally or vertically; they are never at a funny angle.

If you think about it, there are lots of ways to specify the location and size of a rectangle. You could give the location of the center or any of the corners, along with the height and width. Or, you could give the location of opposing corners. The choice is arbitrary, but in any case it will require the same number of parameters: four.

By convention, the usual way to specify a bounding box is to give the location of the upper-left corner and the width and height. The usual way to specify a location is to use a coordinate system.