Source of PolygonsJPanel.java


  1: // Fig. 12.27: PolygonsJPanel.java
  2: // Drawing polygons.
  3: import java.awt.Graphics;
  4: import java.awt.Polygon;
  5: import javax.swing.JPanel;
  6: 
  7: public class PolygonsJPanel extends JPanel 
  8: {
  9:    // draw polygons and polylines
 10:    public void paintComponent( Graphics g )
 11:    {
 12:       super.paintComponent( g ); // call superclass's paintComponent
 13: 
 14:       // draw polygon with Polygon object
 15:       int xValues[] = { 20, 40, 50, 30, 20, 15 };
 16:       int yValues[] = { 50, 50, 60, 80, 80, 60 };
 17:       Polygon polygon1 = new Polygon( xValues, yValues, 6 );
 18:       g.drawPolygon( polygon1 );
 19: 
 20:       // draw polylines with two arrays
 21:       int xValues2[] = { 70, 90, 100, 80, 70, 65, 60 };
 22:       int yValues2[] = { 100, 100, 110, 110, 130, 110, 90 };
 23:       g.drawPolyline( xValues2, yValues2, 7 );
 24: 
 25:       // fill polygon with two arrays
 26:       int xValues3[] = { 120, 140, 150, 190 };
 27:       int yValues3[] = { 40, 70, 80, 60 };
 28:       g.fillPolygon( xValues3, yValues3, 4 );
 29: 
 30:       // draw filled polygon with Polygon object
 31:       Polygon polygon2 = new Polygon();
 32:       polygon2.addPoint( 165, 135 );
 33:       polygon2.addPoint( 175, 150 );
 34:       polygon2.addPoint( 270, 200 );
 35:       polygon2.addPoint( 200, 220 );
 36:       polygon2.addPoint( 130, 180 );
 37:       g.fillPolygon( polygon2 );
 38:    } // end method paintComponent
 39: } // end class PolygonsJPanel
 40: 
 41: /**************************************************************************
 42:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 43:  * Pearson Education, Inc. All Rights Reserved.                           *
 44:  *                                                                        *
 45:  * DISCLAIMER: The authors and publisher of this book have used their     *
 46:  * best efforts in preparing the book. These efforts include the          *
 47:  * development, research, and testing of the theories and programs        *
 48:  * to determine their effectiveness. The authors and publisher make       *
 49:  * no warranty of any kind, expressed or implied, with regard to these    *
 50:  * programs or to the documentation contained in these books. The authors *
 51:  * and publisher shall not be liable in any event for incidental or       *
 52:  * consequential damages in connection with, or arising out of, the       *
 53:  * furnishing, performance, or use of these programs.                     *
 54:  *************************************************************************/