Source of DrawFrame.java


  1: // Fig. 22.4: DrawFrame.java
  2: // Using a customized Panel object.
  3: import java.awt.GridLayout;
  4: import java.awt.BorderLayout;
  5: import java.awt.Color;
  6: import java.awt.event.ActionListener;
  7: import java.awt.event.ActionEvent;
  8: import javax.swing.JFrame;
  9: import javax.swing.JPanel;
 10: import javax.swing.JButton;
 11: 
 12: public class DrawFrame extends JFrame 
 13: {
 14:    private JPanel buttonPanel; // panel to hold the buttons
 15:    private CustomPanel myPanel; // panel for drawing shapes
 16:    private JButton circleButton; // button to create a circle
 17:    private JButton squareButton; // button to create a square
 18: 
 19:    // set up GUI
 20:    public DrawFrame()
 21:    {
 22:       super( "CustomPanel Test" );
 23: 
 24:       myPanel = new CustomPanel(); // create custom drawing area
 25:       myPanel.setBackground( Color.GREEN ); // set color to green
 26: 
 27:       squareButton = new JButton( "Square" ); // button to create square
 28:       squareButton.addActionListener(
 29: 
 30:          new ActionListener() // anonymous inner class 
 31:          {  
 32:             // draw a square
 33:             public void actionPerformed( ActionEvent event )
 34:             {
 35:                myPanel.draw( CustomPanel.Shape.SQUARE ); // draw square
 36:             } // end method actionPerformed
 37:          } // end anonymous inner class
 38:       ); // end call to addActionListener
 39: 
 40:       circleButton = new JButton( "Circle" ); // button to create circle
 41:       circleButton.addActionListener(
 42: 
 43:          new ActionListener() // anonymous inner class
 44:          {   
 45:             // draw a circle
 46:             public void actionPerformed( ActionEvent event )
 47:             {
 48:                myPanel.draw( CustomPanel.Shape.CIRCLE ); // draw circle
 49:             } // end method actionPerformed
 50:          } // end anonymous inner class
 51:       ); // end call to addActionListener
 52: 
 53:       buttonPanel = new JPanel(); // panel to hold buttons
 54:       buttonPanel.setLayout( new GridLayout( 1, 2 ) ); // use gridlayout
 55:       buttonPanel.add( circleButton ); // add circle button
 56:       buttonPanel.add( squareButton ); // add square button
 57: 
 58:       add( myPanel, BorderLayout.CENTER ); // add drawing panel to frame
 59:       add( buttonPanel, BorderLayout.SOUTH ); // add buttons to frame
 60:    } // end DrawFrame constructor
 61: } // end class DrawFrame
 62: 
 63: /**************************************************************************
 64:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 65:  * Pearson Education, Inc. All Rights Reserved.                           *
 66:  *                                                                        *
 67:  * DISCLAIMER: The authors and publisher of this book have used their     *
 68:  * best efforts in preparing the book. These efforts include the          *
 69:  * development, research, and testing of the theories and programs        *
 70:  * to determine their effectiveness. The authors and publisher make       *
 71:  * no warranty of any kind, expressed or implied, with regard to these    *
 72:  * programs or to the documentation contained in these books. The authors *
 73:  * and publisher shall not be liable in any event for incidental or       *
 74:  * consequential damages in connection with, or arising out of, the       *
 75:  * furnishing, performance, or use of these programs.                     *
 76:  *************************************************************************/