public class CustomPanel extends JPanel
1: // Fig. 22.3: CustomPanel.java
2: // A customized JPanel class.
3: import java.awt.Graphics;
4: import javax.swing.JPanel;
5:
6: public class CustomPanel extends JPanel
7: {
8: public static enum Shape { CIRCLE, SQUARE };
9: private Shape shape; // type of shape to draw
10:
11: // use shape to draw an oval or rectangle
12: public void paintComponent( Graphics g )
13: {
14: super.paintComponent( g );
15:
16: if ( shape == Shape.CIRCLE )
17: g.fillOval( 50, 10, 60, 60 ); // draw circle
18: else if ( shape == Shape.SQUARE )
19: g.fillRect( 50, 10, 60, 60 ); // draw square
20: } // end method paintComponent
21:
22: // set shape value and repaint CustomPanel
23: public void draw( Shape shapeToDraw )
24: {
25: shape = shapeToDraw; // set shape that will be drawn
26: repaint(); // repaint the panel to draw new shape
27: } // end method draw
28: } // end class CustomPanel
29:
30:
31:
32: /**************************************************************************
33: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
34: * Pearson Education, Inc. All Rights Reserved. *
35: * *
36: * DISCLAIMER: The authors and publisher of this book have used their *
37: * best efforts in preparing the book. These efforts include the *
38: * development, research, and testing of the theories and programs *
39: * to determine their effectiveness. The authors and publisher make *
40: * no warranty of any kind, expressed or implied, with regard to these *
41: * programs or to the documentation contained in these books. The authors *
42: * and publisher shall not be liable in any event for incidental or *
43: * consequential damages in connection with, or arising out of, the *
44: * furnishing, performance, or use of these programs. *
45: *************************************************************************/