Java Reference
In-Depth Information
The
repaint
method (lines 75, 86) is defined in the
Component
class. Invoking
repaint
causes the
paintComponent
method to be called. The
repaint
method is invoked to
refresh the viewing area. Typically, you call it if you have new things to display.
Caution
The
paintComponent
method should never be invoked directly. It is invoked either by
the JVM whenever the viewing area changes or by the
repaint
method. You should
override the
paintComponent
method to tell the system how to paint the viewing
area, but never override the
repaint
method.
don't invoke
paintComponent
Note
The
repaint
method lodges a request to update the viewing area and returns immedi-
ately. Its effect is asynchronous, meaning that it is up to the JVM to execute the
paintComponent
method on a separate thread.
request repaint using
repaint()
The
getPreferredSize()
method (lines 95-97), defined in
Component
, is overridden
in
FigurePanel
to specify the preferred size for the layout manager to consider when laying
out a
FigurePanel
object. This property may or may not be considered by the layout man-
ager, depending on its rules. For example, a component uses its preferred size in a container
with a
FlowLayout
manager, but its preferred size is ignored if it is placed in a container
with a
GridLayout
manager. It is a good practice to override
getPreferredSize()
in a
subclass of
JPanel
to specify a preferred size, because the default width and height for a
JPanel
is
0
. You will see nothing if a
JPanel
component with a default
0
width and height
is placed in a
FlowLayout
container.
why override
getPreferredSize()
?
13.12
Why should you override the
preferredSize
method in a subclass of
JPanel
?
13.13
How do you
get
and
set
colors and fonts in a
Graphics
object?
✓
✓
Check
Point
An arc is conceived as part of an oval bounded by a rectangle.
Key
Point
The methods to draw or fill an arc are as follows:
drawArc(
int
x,
int
y,
int
w,
int
h,
int
startAngle,
int
arcAngle)
fillArc(
int
x,
int
y,
int
w,
int
h,
int
startAngle,
int
arcAngle)
Parameters
x
,
y
,
w
, and
h
are the same as in the
drawOval
method; parameter
startAngle
is the starting angle; and
arcAngle
is the spanning angle (i.e., the angle covered by the arc).
Angles are measured in degrees and follow the usual mathematical conventions (i.e.,
0
degrees is in the easterly direction, and positive angles indicate counterclockwise rotation
from the easterly direction); see Figure 13.10.
(x, y)
w
arcAngle
startAngle
h
F
IGURE
13.10
The
drawArc
method draws an arc based on an oval with specified angles.