Java Reference
In-Depth Information
Because both
y
-coordinates
(Y_MOUTH)
are the same, the line is horizontal. The line
for the mouth begins at coordinates
(X_MOUTH, Y_MOUTH)
and extends to the right for
MOUTH_WIDTH
pixels.
The
Graphics
Class
Every container and component that can be drawn on the screen 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.
When an object
g
of the class
Graphics
is used as the calling object for a drawing method,
the drawing takes place inside the area of the screen specified by
g.
For example, if
g
is
the
Graphics
object for a
JFrame,
the drawing takes place inside the borders of the
JFrame
object.
Some of the commonly used methods of the class
Graphics
are given in Display 18.12.
Graphics
is an abstract class in the
java.awt
package.
Some of the commonly used methods of the class
Graphics
are given in Display 18.12 .
Note that most methods come in pairs, one whose name starts with
draw
and one whose
name starts with
fill,
such as
drawOval
and
fillOval.
The one that starts with
draw
will draw the outline of the specified figure. The one that starts with
fill
will draw a solid
figure obtained by filling the inside of the specified figure. In the next few subsections, we
discuss some of these methods.
Display 18.12
Some Methods in the Class
Graphics
(part 1 of 3)
Graphics
is an abstract class in the
java.awt
package
.
Although many of these methods are abstract, we always use them with objects of a concrete
descendent class of
Graphics
, even though we usually do not know the name of that
concrete class.
public abstract void
drawLine(
int
x1,
int
y1,
int x2
,
int
y2)
Draws a line between points (
x1, y1
) and (
x2, y2
).
public abstract void
drawRect(
int
x,
int
y,
int
width,
int
height)
Draws the outline of the specified rectangle. (
x, y
) is the location of the upper-left corner of the
rectangle.
public abstract void
fillRect
(
int
x,
int
y,
int
width,
int
height)
Fills the specified rectangle. (x, y) is the location of the upper-left corner of the rectangle.
(continued)