Share
Explore BrainMass

Java Script: Is it possible to increase/decrease the diameter of the inner circle of stars?

// Fig. 12.31: Shapes2JPanel.java
// Demonstrating a general path.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.util.Random;
import javax.swing.JPanel;

public class Shapes2JPanel extends JPanel
{
// draw general paths
public void paintComponent( Graphics g )
{
super.paintComponent( g ); // call superclass's paintComponent
Random random = new Random(); // get random number generator

int[] xPoints = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43,56,78,90 };
int[] yPoints = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36,78,68,35 };

Graphics2D g2d = ( Graphics2D ) g;
GeneralPath star = new GeneralPath(GeneralPath.WIND_EVEN_ODD, xPoints.length); // create GeneralPath object

// set the initial coordinate of the General Path
star.moveTo( xPoints[ 9 ], yPoints[ 9 ] );

// create the star--this does not draw the star
for ( int count = 1; count < xPoints.length; count++ )
star.lineTo( xPoints[ count ], yPoints[ count ] );

star.closePath(); // close the shape

g2d.translate( 500, 500 ); // translate the origin to (150, 150)

// rotate around origin and draw stars in random colors
for ( int count = 1; count <= 20; ++count )
{
g2d.rotate( Math.PI / 10.0 ); // rotate coordinate system

// set random drawing color
g2d.setColor( new Color( random.nextInt( 256 ),
random.nextInt( 256 ), random.nextInt( 256 ) ) );

g2d.fill( star ); // draw filled star
} // end for
} // end method paintComponent
} // end class Shapes2JPanel

/**************************************************************************
* (C) Copyright 1992-2012 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/

--------------------------------------------------------------------
---DRIVER---
// Fig. 12.32: Shapes2.java
// Demonstrating a general path.
import java.awt.Color;
import javax.swing.JFrame;

public class Shapes2
{
// execute application
public static void main( String[] args )
{
// create frame for Shapes2JPanel
JFrame frame = new JFrame( "Drawing 2D Shapes" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

Shapes2JPanel shapes2JPanel = new Shapes2JPanel();
frame.add( shapes2JPanel ); // add shapes2JPanel to frame
frame.setBackground( Color.WHITE ); // set frame background color
frame.setSize( 1200, 1000 ); // set frame size
frame.setVisible( true ); // display frame
} // end main
} // end class Shapes2

/**************************************************************************
* (C) Copyright 1992-2012 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/

Solution Summary

The expert determines whether it is possible to increase/decrease the diameter of the inner circle of stars.

$2.19