Source of Shapes.java


  1: // Fig. 5.26: Shapes.java
  2: // Demonstrates drawing different shapes.
  3: import java.awt.Graphics;
  4: import javax.swing.JPanel;

  6: public class Shapes extends JPanel
  7: {
  8:    private int choice; // user's choice of which shape to draw
  9:    
 10:    // constructor sets the user's choice
 11:    public Shapes( int userChoice )
 12:    {
 13:       choice = userChoice;
 14:    } // end Shapes constructor
 15:    
 16:    // draws a cascade of shapes starting from the top left corner
 17:    public void paintComponent( Graphics g )
 18:    {
 19:       super.paintComponent( g );
 20:       
 21:       for ( int i = 0; i < 10; i++ )
 22:       {
 23:          // pick the shape based on the user's choice
 24:          switch ( choice )
 25:          {
 26:             case 1: // draw rectangles
 27:                g.drawRect( 10 + i * 10, 10 + i * 10, 
 28:                   50 + i * 10, 50 + i * 10 );
 29:                break;
 30:             case 2: // draw ovals
 31:                g.drawOval( 10 + i * 10, 10 + i * 10, 
 32:                   50 + i * 10, 50 + i * 10 );
 33:                break;
 34:          } // end switch
 35:       } // end for
 36:    } // end method paintComponent
 37: } // end class Shapes


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