Java Reference
In-Depth Information
their joining will be rounded ( BasicStroke.JOIN_ROUND ). The dashes argument specifies
the dash lengths for the line. The last argument indicates the starting index in the dashes
array for the first dash in the pattern. Line 73 then draws a line with the current Stroke .
Creating Your Own Shapes with General Paths
Next we present a general path —a shape constructed from straight lines and complex
curves. A general path is represented with an object of class GeneralPath (package ja-
va.awt.geom ). The application of Figs. 13.31 and 13.32 demonstrates drawing a general
path in the shape of a five-pointed star.
1
// Fig. 13.31: Shapes2JPanel.java
2
// Demonstrating a general path.
3
import java.awt.Color;
4
import java.awt.Graphics;
5
import java.awt.Graphics2D;
6
import java.awt.geom.GeneralPath;
7
import java.security.SecureRandom;
8
import javax.swing.JPanel;
9
10
public class Shapes2JPanel extends JPanel
11
{
12
// draw general paths
13
@Override
14
public void paintComponent(Graphics g)
15
{
16
super .paintComponent(g);
17
SecureRandom random = new SecureRandom();
18
19
int [] xPoints = { 55 , 67 , 109 , 73 , 83 , 55 , 27 , 37 , 1 , 43 };
20
int [] yPoints = { 0 , 36 , 36 , 54 , 96 , 72 , 96 , 54 , 36 , 36 };
21
22
Graphics2D g2d = (Graphics2D) g;
23
GeneralPath star = new GeneralPath();
24
25
// set the initial coordinate of the General Path
star.moveTo(xPoints[ 0 ], yPoints[ 0 ]);
26
27
28
// create the star--this does not draw the star
29
for ( int count = 1 ; count < xPoints.length; count++)
30
star.lineTo(xPoints[count], yPoints[count]);
31
32
star.closePath(); // close the shape
33
34
g2d.translate( 150 , 150 ); // translate the origin to (150, 150)
35
36
// rotate around origin and draw stars in random colors
37
for ( int count = 1 ; count <= 20 ; count++)
38
{
39
g2d.rotate( Math.PI / 10.0 ); // rotate coordinate system
40
Fig. 13.31 | Java 2D general paths. (Part 1 of 2.)
 
Search WWH ::




Custom Search