Java Reference
In-Depth Information
•
void fillOval(int x, int y, int width, int height)
drawsafilledovalinthecurrentcolorsuchthattheovalfitswithinthebound-
ing box whose upper-left corner is at (
x
,
y
) and whose extents are (
width
,
height
).
•
void fillRect(int x, int y, int width, int height)
drawsafilledrectangleinthecurrentcolorwhoseupper-leftcornerisat(
x
,
y
)
andwhoseextentsare(
width
,
height
),suchthattherightedgeislocatedat
x+width-1
and the bottom edge is located at
y+height-1
.
State methods include the following:
•
void setColor(Color c)
sets the current color to the
java.awt.Color
instancepassedto
c
.
Color
declaresseveraluppercase/
lowercase
Color
constants for common colors (e.g.,
RED
/
red
,
GREEN
/
green
,and
BLUE
/
blue
)andconstructorsfordescribingarbitrarycol-
ors—it'sconventionaltousetheuppercasecolorconstants.Acompanion
Co-
lor getColor()
method returns the current color.
•
void setFont(Font f)
setsthecurrentfonttothe
java.awt.Font
in-
stancepassedto
f
.Acompanion
Font getFont()
methodreturnsthecur-
rent font.
The following example demonstrates various drawing and state methods:
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.drawLine(10, 10, 20, 20);
g.setFont(new Font("Arial", Font.BOLD, 10));
g.drawString("Hello", 35, 35);
}
The first statement sets the current color to
Color.RED
and the second statement
drawsalineinthiscolorfromstartingpoint(
10
,
10
)toendingpoint(
20
,
20
).(When
you don't specify a color before drawing, the color defaults to the component's back-
ground color, which is returned from
Component
's
Color getBackground()
method.)
The third statement calls
Font
's
Font(String name, int style, int
size)
constructor to create a
Font
object that describes a font named
Arial
with
style
BOLD
andpointsize
10
—a
point
isatypographicmeasurementthat'sapproxim-