Java Reference
In-Depth Information
not want to lose the original function of
paintComponent
, namely to draw the
panel itself, especially its background. Hence we call the original method using
!
the
super
command. This has to be the
first
command in our implementation
of
paintComponent
. Then our own graphic commands follow. Hence we have the
following generic structure:
public void
paintComponent(Graphics g) {
super
.paintComponent(g);
//
Own graphics command
//
which draws on the panel
.
}
We are then sure that first the empty panel is drawn, and then our drawing is
displayed. In particular, any existing old drawing is erased before our drawing is
begun.
5.2
■
The graphics commands
The class
Graphics
supplies a number of commands for drawing. We present only
a few - further commands can be found in the Java documentation.
drawLine(
int
xstart,
int
ystart,
int
xend,
int
yend)
drawRect(
int
xleft,
int
ytop,
int
width,
int
height)
drawOval(
int
xleft,
int
ytop,
int
width,
int
height)
drawString(String text,
int
xleft,
int
ybottom)
fillRect(
int
xleft,
int
ytop,
int
width,
int
height)
fillOval(
int
xleft,
int
ytop,
int
width,
int
height)
setColor(Color col)
We explain the commands below; see also Figure 5.1. The coordinates are in pixels
in the Java coordinate system. The shapes are drawn in the order in which the
graphic commands are executed. If shapes overlap, the one drawn later covers
those drawn previously.
drawLine(xstart,ystart,xend,yend)
draws a line segment between the points
with coordinates (
xstart
,
ystart
) and (
xend
,
yend
).
drawRect(xleft,ytop,width,height)
draws the contour of an axes-aligned
rectangle. The upper left corner of the rectangle is at
(xleft, ytop)
.Itis
w
idth
pixels wide and
height
pixels tall.
drawOval(xleft,ytop,width,height)
draws the contour of an ellipse. The axes
of the ellipse are parallel to the coordinate axes. The upper left corner of the
bounding rectangle of the ellipse is at
(xleft, ytop)
and is called the
reference
point
. The horizontal axis of the ellipse has length
w
idth
, the vertical one
height
.
Search WWH ::
Custom Search