Source of ShapesJPanel.java


  1: // Fig. 12.29: ShapesJPanel.java
  2: // Demonstrating some Java 2D shapes.
  3: import java.awt.Color;
  4: import java.awt.Graphics;
  5: import java.awt.BasicStroke;
  6: import java.awt.GradientPaint;
  7: import java.awt.TexturePaint;
  8: import java.awt.Rectangle;
  9: import java.awt.Graphics2D;
 10: import java.awt.geom.Ellipse2D;
 11: import java.awt.geom.Rectangle2D;
 12: import java.awt.geom.RoundRectangle2D;
 13: import java.awt.geom.Arc2D;
 14: import java.awt.geom.Line2D;
 15: import java.awt.image.BufferedImage;
 16: import javax.swing.JPanel;
 17: 
 18: public class ShapesJPanel extends JPanel 
 19: {
 20:    // draw shapes with Java 2D API
 21:    public void paintComponent( Graphics g )
 22:    {
 23:       super.paintComponent( g ); // call superclass's paintComponent
 24: 
 25:       Graphics2D g2d = ( Graphics2D ) g; // cast g to Graphics2D
 26: 
 27:       // draw 2D ellipse filled with a blue-yellow gradient
 28:       g2d.setPaint( new GradientPaint( 5, 30, Color.BLUE, 35, 100, 
 29:          Color.YELLOW, true ) );  
 30:       g2d.fill( new Ellipse2D.Double( 5, 30, 65, 100 ) );
 31: 
 32:       // draw 2D rectangle in red
 33:       g2d.setPaint( Color.RED );                  
 34:       g2d.setStroke( new BasicStroke( 10.0f ) ); 
 35:       g2d.draw( new Rectangle2D.Double( 80, 30, 65, 100 ) );
 36: 
 37:       // draw 2D rounded rectangle with a buffered background
 38:       BufferedImage buffImage = new BufferedImage( 10, 10, 
 39:          BufferedImage.TYPE_INT_RGB );
 40: 
 41:       // obtain Graphics2D from bufferImage and draw on it
 42:       Graphics2D gg = buffImage.createGraphics();   
 43:       gg.setColor( Color.YELLOW ); // draw in yellow
 44:       gg.fillRect( 0, 0, 10, 10 ); // draw a filled rectangle
 45:       gg.setColor( Color.BLACK );  // draw in black
 46:       gg.drawRect( 1, 1, 6, 6 ); // draw a rectangle
 47:       gg.setColor( Color.BLUE ); // draw in blue
 48:       gg.fillRect( 1, 1, 3, 3 ); // draw a filled rectangle
 49:       gg.setColor( Color.RED ); // draw in red
 50:       gg.fillRect( 4, 4, 3, 3 ); // draw a filled rectangle
 51: 
 52:       // paint buffImage onto the JFrame
 53:       g2d.setPaint( new TexturePaint( buffImage, 
 54:          new Rectangle( 10, 10 ) ) );
 55:       g2d.fill(
 56:          new RoundRectangle2D.Double( 155, 30, 75, 100, 50, 50 ) );
 57: 
 58:       // draw 2D pie-shaped arc in white
 59:       g2d.setPaint( Color.WHITE );
 60:       g2d.setStroke( new BasicStroke( 6.0f ) ); 
 61:       g2d.draw(
 62:          new Arc2D.Double( 240, 30, 75, 100, 0, 270, Arc2D.PIE ) );
 63: 
 64:       // draw 2D lines in green and yellow
 65:       g2d.setPaint( Color.GREEN );
 66:       g2d.draw( new Line2D.Double( 395, 30, 320, 150 ) );
 67: 
 68:       // draw 2D line using stroke
 69:       float dashes[] = { 10 }; // specify dash pattern
 70:       g2d.setPaint( Color.YELLOW );    
 71:       g2d.setStroke( new BasicStroke( 4, BasicStroke.CAP_ROUND,
 72:          BasicStroke.JOIN_ROUND, 10, dashes, 0 ) ); 
 73:       g2d.draw( new Line2D.Double( 320, 30, 395, 150 ) );
 74:    } // end method paintComponent
 75: } // end class ShapesJPanel
 76: 
 77: /**************************************************************************
 78:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 79:  * Pearson Education, Inc. All Rights Reserved.                           *
 80:  *                                                                        *
 81:  * DISCLAIMER: The authors and publisher of this book have used their     *
 82:  * best efforts in preparing the book. These efforts include the          *
 83:  * development, research, and testing of the theories and programs        *
 84:  * to determine their effectiveness. The authors and publisher make       *
 85:  * no warranty of any kind, expressed or implied, with regard to these    *
 86:  * programs or to the documentation contained in these books. The authors *
 87:  * and publisher shall not be liable in any event for incidental or       *
 88:  * consequential damages in connection with, or arising out of, the       *
 89:  * furnishing, performance, or use of these programs.                     *
 90:  *************************************************************************/