Source of CustomFrame.java


  1: // Fig. 22.7: CustomFrame.java
  2: // Creating a custom frame that contains components 
  3: // for testing SelfContainedPanel.
  4: import java.awt.Color;
  5: import java.awt.FlowLayout;
  6: import java.awt.event.MouseMotionListener;
  7: import java.awt.event.MouseEvent;
  8: import javax.swing.JFrame;
  9: import com.deitel.jhtp6.ch22.SelfContainedPanel;
 10: 
 11: public class CustomFrame extends JFrame 
 12: {
 13:    private SelfContainedPanel myPanel; // panel to draw an oval
 14: 
 15:    // set up GUI and mouse motion event handlers for application window
 16:    public CustomFrame()
 17:    {
 18:       myPanel = new SelfContainedPanel(); // create self contained panel
 19:       myPanel.setBackground( Color.YELLOW ); // set background to yellow
 20: 
 21:       setLayout( new FlowLayout() ); // set frame layout
 22:       add( myPanel ); // add self contained panel to frame
 23: 
 24:       // set up mouse motion event handling
 25:       addMouseMotionListener(
 26: 
 27:          new MouseMotionListener() // anonymous inner class
 28:          {  
 29:             // handle mouse drag event
 30:             public void mouseDragged( MouseEvent event )
 31:             {
 32:                setTitle( String.format( "Dragging: x=%d; y=%d",
 33:                   event.getX(), event.getY() ) ); // display dragging
 34:             } // end method mouseDragged
 35: 
 36:             // handle mouse move event
 37:             public void mouseMoved( MouseEvent event )
 38:             {
 39:                setTitle( String.format( "Moving: x=%d; y=%d", 
 40:                   event.getX(), event.getY() ) ); // display moving
 41:             } // end method mouseMoved
 42:          } // end anonymous inner class
 43:       ); // end call to addMouseMotionListener
 44:    } // end CustomFrame constructor
 45: } // end class CustomFrame
 46: 
 47: /**************************************************************************
 48:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 49:  * Pearson Education, Inc. All Rights Reserved.                           *
 50:  *                                                                        *
 51:  * DISCLAIMER: The authors and publisher of this book have used their     *
 52:  * best efforts in preparing the book. These efforts include the          *
 53:  * development, research, and testing of the theories and programs        *
 54:  * to determine their effectiveness. The authors and publisher make       *
 55:  * no warranty of any kind, expressed or implied, with regard to these    *
 56:  * programs or to the documentation contained in these books. The authors *
 57:  * and publisher shall not be liable in any event for incidental or       *
 58:  * consequential damages in connection with, or arising out of, the       *
 59:  * furnishing, performance, or use of these programs.                     *
 60:  *************************************************************************/