Source of PaintPanel.java


  1: // Fig. 11.32: PaintPanel.java
  2: // Using class MouseMotionAdapter.
  3: import java.awt.Point;
  4: import java.awt.Graphics;
  5: import java.awt.event.MouseEvent;
  6: import java.awt.event.MouseMotionAdapter;
  7: import javax.swing.JPanel;
  8: 
  9: public class PaintPanel extends JPanel 
 10: {
 11:    private int pointCount = 0; // count number of points
 12: 
 13:    // array of 10000 java.awt.Point references
 14:    private Point points[] = new Point[ 10000 ];  
 15: 
 16:    // set up GUI and register mouse event handler
 17:    public PaintPanel()
 18:    {
 19:       // handle frame mouse motion event
 20:       addMouseMotionListener(
 21: 
 22:          new MouseMotionAdapter() // anonymous inner class
 23:          {  
 24:             // store drag coordinates and repaint
 25:             public void mouseDragged( MouseEvent event )
 26:             {
 27:                if ( pointCount < points.length ) 
 28:                {
 29:                   points[ pointCount ] = event.getPoint(); // find point
 30:                   pointCount++; // increment number of points in array
 31:                   repaint(); // repaint JFrame
 32:                } // end if
 33:             } // end method mouseDragged
 34:          } // end anonymous inner class
 35:       ); // end call to addMouseMotionListener
 36:    } // end PaintPanel constructor
 37: 
 38:    // draw oval in a 4-by-4 bounding box at specified location on window
 39:    public void paintComponent( Graphics g )
 40:    {
 41:       super.paintComponent( g ); // clears drawing area
 42: 
 43:       // draw all points in array
 44:       for ( int i = 0; i < pointCount; i++ )
 45:          g.fillOval( points[ i ].x, points[ i ].y, 4, 4 );
 46:    } // end method paint
 47: } // end class PaintPanel
 48: 
 49: 
 50: /**************************************************************************
 51:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 52:  * Pearson Education, Inc. All Rights Reserved.                           *
 53:  *                                                                        *
 54:  * DISCLAIMER: The authors and publisher of this book have used their     *
 55:  * best efforts in preparing the book. These efforts include the          *
 56:  * development, research, and testing of the theories and programs        *
 57:  * to determine their effectiveness. The authors and publisher make       *
 58:  * no warranty of any kind, expressed or implied, with regard to these    *
 59:  * programs or to the documentation contained in these books. The authors *
 60:  * and publisher shall not be liable in any event for incidental or       *
 61:  * consequential damages in connection with, or arising out of, the       *
 62:  * furnishing, performance, or use of these programs.                     *
 63:  *************************************************************************/