Java Reference
In-Depth Information
41
// set random drawing color
42
g2d.setColor(
new
Color(random.nextInt(
256
),
43
random.nextInt(
256
), random.nextInt(
256
)));
44
45
g2d.fill(star);
// draw filled star
46
}
47
}
48
}
// end class Shapes2JPanel
Fig. 13.31
|
Java 2D general paths. (Part 2 of 2.)
1
// Fig. 13.32: Shapes2.java
2
// Demonstrating a general path.
3
import
java.awt.Color;
4
import
javax.swing.JFrame;
5
6
public
class
Shapes2
7
{
8
// execute application
9
public
static
void
main(String[] args)
10
{
11
// create frame for Shapes2JPanel
12
JFrame frame =
new
JFrame(
"Drawing 2D Shapes"
);
13
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);
14
15
Shapes2JPanel shapes2JPanel =
new
Shapes2JPanel();
16
frame.add(shapes2JPanel);
17
frame.setBackground(
Color.WHITE
);
18
frame.setSize(
315
,
330
);
19
frame.setVisible(
true
);
20
}
21
}
// end class Shapes2
Fig. 13.32
|
Demonstrating a general path.
Lines 19-20 (Fig. 13.31) declare two
int
arrays representing the
x-
and
y
-coordinates
of the points in the star. Line 23 creates
GeneralPath
object
star
. Line 26 uses
General-
Path
method
moveTo
to specify the first point in the
star
. The
for
statement in lines 29-