Source of OvalPanel.java


  1: // Fig. 22.2: OvalPanel.java
  2: // A customized JPanel class.
  3: import java.awt.Graphics;
  4: import java.awt.Dimension;
  5: import javax.swing.JPanel;
  6: 
  7: public class OvalPanel extends JPanel 
  8: {
  9:    private int diameter = 10; // default diameter of 10
 10: 
 11:    // draw an oval of the specified diameter
 12:    public void paintComponent( Graphics g )
 13:    {
 14:       super.paintComponent( g );
 15: 
 16:       g.fillOval( 10, 10, diameter, diameter ); // draw circle
 17:    } // end method paintComponent
 18: 
 19:    // validate and set diameter, then repaint 
 20:    public void setDiameter( int newDiameter )
 21:    {
 22:       // if diameter invalid, default to 10
 23:       diameter = ( newDiameter >= 0 ? newDiameter : 10 );
 24:       repaint(); // repaint panel
 25:    } // end method setDiameter
 26: 
 27:    // used by layout manager to determine preferred size
 28:    public Dimension getPreferredSize()
 29:    {
 30:       return new Dimension( 200, 200 );
 31:    } // end method getPreferredSize
 32: 
 33:    // used by layout manager to determine minimum size
 34:    public Dimension getMinimumSize()
 35:    {
 36:       return getPreferredSize();
 37:    } // end method getMinimumSize
 38: } // end class OvalPanel
 39: 
 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:  *************************************************************************/