Java
Java
java
2 // Demonstrating a general path
34
// Java core packages
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.awt.geom.*;
89
// Java extension packages
10 import javax.swing.*;
11
12 public class Shapes2 extends JFrame {
13
14 // set window's title bar String, background color
15 // and dimensions
16 public Shapes2()
17 {
18 super( "Drawing 2D Shapes" );
19
20 getContentPane().setBackground( Color.yellow );
21 setSize( 400, 400 );
22 setVisible( true );
23 }
24
25 // draw general paths
26 public void paint( Graphics g )
27 {
28 // call superclass's paint method
29 super.paint( g );
30
31 int xPoints[] =
32 { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
33 int yPoints[] =
34 { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };
35
36 Graphics2D g2d = ( Graphics2D ) g;
37
38 // create a star from a series of points
39 GeneralPath star = new GeneralPath();
40
41 // set the initial coordinate of the General Path
42 star.moveTo( xPoints[ 0 ], yPoints[ 0 ] );
43
44 // create the star--this does not draw the star
45 for ( int count = 1; count < xPoints.length; count++ )
46 star.lineTo( xPoints[ count ], yPoints[ count ] );
47
48 // close the shape
49 star.closePath();
50
51 // translate the origin to (200, 200)
52 g2d.translate( 200, 200 );
54 // rotate around origin and draw stars in random colors
55 for ( int count = 1; count <= 20; count++ ) {
56
57 // rotate coordinate system
58 g2d.rotate( Math.PI / 10.0 );
59
60 // set random drawing color
61 g2d.setColor( new Color(
62 ( int ) ( Math.random() * 256 ),
63 ( int ) ( Math.random() * 256 ),
64 ( int ) ( Math.random() * 256 ) ) );
65
66 // draw filled star
67 g2d.fill( star );
68 }
69
70 } // end method paint
71
72 // execute application
73 public static void main( String args[] )
74 {
75 Shapes2 application = new Shapes2();
76
77 application.setDefaultCloseOperation(
78 JFrame.EXIT_ON_CLOSE );
79 }
80
81 } // end class Shapes2