Java Reference
In-Depth Information
Notice that the method paint has a parameter g of type Graphics . Graphics is an
abstract class in the java.awt package. Every container and component that can be
drawn on the screen has an associated Graphics object. (To be precise, every JComponent
has an associated Graphics object.) This associated Graphics object has data specifying
what area of the screen the component or container covers. In particular, the Graphics
object for a JFrame specifies that the drawing takes place inside the borders of the JFrame
object. (Since Graphics is an abstract class, every Graphics object is an instance of some
concrete descendent class of the Graphics class, but we usually do not care about which
descendent class. All we normally need to know is that it is of type Graphics .)
The Graphics class, and so any Graphics object g , has all the methods that we will
use to draw figures, such as circles, lines, and boxes, on the screen. Almost the entire
definition of the paint method in Display 18.11 consists of invocations of various
drawing methods with the parameter g as the calling object.
When the paint method in Display 18.11 is (automatically) invoked, the parame-
ter g will be replaced by the Graphics object associated with the JFrame , so the figures
drawn will be inside the JFrame . Let's look at the code in this method paint .
Notice the first line in the definition of paint in Display 18.11:
Graphics
super .paint(g);
Recall that super is a name for the parent class of a derived class. The class in Display
18.11 is derived from the class JFrame , so super.paint is the paint method for the class
JFrame . Whenever you redefine the method paint , you should start with this invocation
of super.paint . This ensures that your definition of paint will do all the things the
standard paint method does, such as draw the title and border for the JFrame . (This les-
son applies even if the class is derived from some class other than JFrame .)
The following invocation from the method paint draws the circle forming the
head:
drawOval
g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
The last two arguments give the width and height of the enclosing rectangle, shown in
red. The fact that these two arguments are equal is what makes it a circle instead of a
typical oval. The first two arguments give x - and y -coordinates for the position of the
circle. Note that a figure is positioned by giving the position of the upper-left corner of
an enclosing rectangle.
The only other drawing statements in the method paint are invocations of
g.drawLine . The method g.drawLine draws a straight line between two points with x -
and y -coordinates ( x 1 , y 1 ) and ( x 2 , y 2 ), where the argument positions for the four coor-
dinate numbers are indicated below:
drawLine
g.drawLine( x 1 , y 1 , x 2 , y 2 )
For example, the invocation that draws the mouth is as follows:
g.drawLine(X_MOUTH, Y_MOUTH, X_MOUTH + MOUTH_WIDTH, Y_MOUTH);
Search WWH ::




Custom Search