Java Reference
In-Depth Information
The argument passed to this method describes a
graphics context
, an object created
fromaconcretesubclassoftheabstract
java.awt.Graphics
class.Thisobjectde-
scribes a
drawing surface
on which pixels are drawn (e.g., a monitor screen, a printer
page, or an image buffer).
Thedrawingsurfacehasatwo-dimensionalcoordinatesystemwithits(0,0)originin
theupper-leftcorner,itshorizontal(X)axispositivelyincreasingfromlefttoright,and
itsvertical(Y)axispositivelyincreasingfromtoptobottom.
Figure7-7
illustratesthis
coordinate system.
Figure 7-7.
A drawing surface's coordinate system is anchored in an origin at its upper-
left corner.
Graphics
declaresvariousmethodsfordrawingonthesurfaceandsettingcontext
state. Its drawing methods include the following:
•
void drawLine(int x1, int y1, int x2, int y2)
drawsaline
in the current color from (
x1
,
y1
) to (
x2
,
y2
).
•
void drawOval(int x, int y, int width, int height)
draws the outline of an oval in the current color such that the oval fits within
the
bounding box
(smallest enclosing rectangle) whose upper-left corner is at
(
x
,
y
)andwhoseextentsare(
width
,
height
).Theovalcoversanareathat
is
width+1
pixels wide and
height+1
pixels tall.
•
void drawRect(int x, int y, int width, int height)
drawstheoutlineofarectangleinthecurrentcolorwhoseupper-leftcorneris
at(
x
,
y
)andwhoseextentsare(
width
,
height
),suchthattherightedgeis
located at
x+width
and the bottom edge is located at
y+height
.
•
void drawString(String str, int x, int y)
drawsthechar-
acters specified by
str
in the current color and using the current font. The
baseline of the leftmost character is at (
x
,
y
).