Source of Shapes2JPanel.java


  1: // Fig. 12.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.util.Random;
  8: import javax.swing.JPanel;
  9: 
 10: public class Shapes2JPanel extends JPanel 
 11: {
 12:    // draw general paths
 13:    public void paintComponent( Graphics g )
 14:    {
 15:       super.paintComponent( g ); // call superclass's paintComponent
 16:       Random random = new Random(); // get random number generator
 17: 
 18:       int xPoints[] = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
 19:       int yPoints[] = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };
 20: 
 21:       Graphics2D g2d = ( Graphics2D ) g;
 22:       GeneralPath star = new GeneralPath(); // create GeneralPath object
 23: 
 24:       // set the initial coordinate of the General Path
 25:       star.moveTo( xPoints[ 0 ], yPoints[ 0 ] );
 26: 
 27:       // create the star--this does not draw the star
 28:       for ( int count = 1; count < xPoints.length; count++ )
 29:          star.lineTo( xPoints[ count ], yPoints[ count ] );
 30: 
 31:       star.closePath(); // close the shape
 32: 
 33:       g2d.translate( 200, 200 ); // translate the origin to (200, 200)
 34: 
 35:       // rotate around origin and draw stars in random colors
 36:       for ( int count = 1; count <= 20; count++ ) 
 37:       {
 38:          g2d.rotate( Math.PI / 10.0 ); // rotate coordinate system
 39: 
 40:          // set random drawing color
 41:          g2d.setColor( new Color( random.nextInt( 256 ),
 42:             random.nextInt( 256 ), random.nextInt( 256 ) ) );
 43: 
 44:          g2d.fill( star ); // draw filled star
 45:       } // end for
 46:    } // end method paintComponent
 47: } // end class Shapes2JPanel
 48: 
 49: /**************************************************************************
 50:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 51:  * Pearson Education, Inc. All Rights Reserved.                           *
 52:  *                                                                        *
 53:  * DISCLAIMER: The authors and publisher of this book have used their     *
 54:  * best efforts in preparing the book. These efforts include the          *
 55:  * development, research, and testing of the theories and programs        *
 56:  * to determine their effectiveness. The authors and publisher make       *
 57:  * no warranty of any kind, expressed or implied, with regard to these    *
 58:  * programs or to the documentation contained in these books. The authors *
 59:  * and publisher shall not be liable in any event for incidental or       *
 60:  * consequential damages in connection with, or arising out of, the       *
 61:  * furnishing, performance, or use of these programs.                     *
 62:  *************************************************************************/