Java Reference
In-Depth Information
g.setColor(Color.BLACK);
39
40
g.drawLine
(
10
,
10
, width -
10
, height -
10
);
draw lines
g.drawLine
41
(width -
10
,
10
,
10
, height -
10
);
42
break
;
43
case
RECTANGLE:
// Display a rectangle
44 g.setColor(Color.BLUE);
45
46 ((
int
)(
0.1
* width), (
int
)(
0.1
* height),
47 (
int
)(
0.8
* width), (
int
)(
0.8
* height));
48
else
49 ((
int
)(
0.1
* width), (
int
)(
0.1
* height),
50 (
int
)(
0.8
* width), (
int
)(
0.8
* height));
51
break
;
52
case
ROUND_RECTANGLE:
// Display a round-cornered rectangle
53 g.setColor(Color.RED);
54
55 ((
int
)(
0.1
* width), (
int
)(
0.1
* height),
56 (
int
)(
0.8
* width), (
int
)(
0.8
* height),
20
,
20
);
57
else
58 ((
int
)(
0.1
* width), (
int
)(
0.1
* height),
59 (
int
)(
0.8
* width), (
int
)(
0.8
* height),
20
,
20
);
60
break
;
61
case
OVAL:
// Display an oval
62 g.setColor(Color.BLACK);
63
64 ((
int
)(
0.1
* width), (
int
)(
0.1
* height),
65 (
int
)(
0.8
* width), (
int
)(
0.8
* height));
66
else
67 ((
int
)(
0.1
* width), (
int
)(
0.1
* height),
68 (
int
)(
0.8
* width), (
int
)(
0.8
* height));
69 }
70 }
71
72
if
(filled)
g.fillRect
fill a rectangle
g.drawRect
draw a rectangle
if
(filled)
g.fillRoundRect
fill round-cornered rect
g.drawRoundRect
draw round-cornered rect
if
(filled)
g.fillOval
fill an oval
g.drawOval
draw an oval
/** Set a new figure type */
73
public void
setType(
int
type) {
74
this
.type = type;
repaint();
75
76 }
77
78
repaint panel
/** Return figure type */
79
public int
getType() {
80
return
type;
81 }
82
83
/** Set a new filled property */
84
public void
setFilled(
boolean
filled) {
85
this
.filled = filled;
86
87 }
88
89
repaint();
repaint panel
/** Check if the figure is filled */
90
public boolean
isFilled() {
91
return
filled;
92 }
93
94 @Override
// Specify preferred size
95
96
97
98 }
public
Dimension getPreferredSize() {
override
getPreferredSize()
return new
Dimension(
80
,
80
);
}