import java.awt.*;
import java.awt.event.*;
import javabook.*;

/**
 *   class MySketchPad
 *
 *   An alternative implementation of the SkethcPad class
 *   from the javabook package.
 */

class MySketchPad extends MainWindow implements MouseListener, MouseMotionListener
{

//----------------------------------
//    Data Members
//----------------------------------
    
    /**
     * Default frame width
     */
    private static final int FRAME_WIDTH    = 300;
    
    /**
     * Default frame height
     */
    private static final int FRAME_HEIGHT   = 200;
    
    /**
     * X coordinate of the frame default origin point
     */
    private static final int FRAME_X_ORIGIN = 150;
    
    /**
     * Y coordinate of the frame default origin point
     */
    private static final int FRAME_Y_ORIGIN = 250;

    /**
     * X coordinate of the last drag point
     */
    private int last_x;
   
    /**
     * Y coordinate of the last drag point
     */
    private int last_y;

//----------------------------------
//    Constructors
//----------------------------------
                                
    /**
     * Default constructor
     */
   public MySketchPad()
   {
      //set frame properties
      setTitle     ("SketchPad For YOur Doodle Art");
      setSize      (FRAME_WIDTH, FRAME_HEIGHT);
      setResizable (false);
      setLocation  (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);

      last_x = last_y = 0;

      addMouseListener( this );
      addMouseMotionListener( this );
   }

//-------------------------------------------------
//      Public Methods:
// 
//          void    mousePressed   (   MouseEvent    )
//
//          void    mouseDragged   (   MouseEvent    )
//
//------------------------------------------------
                                    

/******************************
    Mouse Event Handling
*******************************/

    /**
     * Standard method to respond MousePressed events.
     * 
     * @param e the MouseEvent object
     *
     */
    public void mousePressed(MouseEvent e)
   {
      if (e.isMetaDown()) {
         //erase the content if it is a rightbutton
         Graphics g = getGraphics();
         Rectangle r = getBounds();
         g.clearRect(0, 0, r.width, r.height);
         g.dispose();
      }
      else {
         //reset for a new mouse drag
         last_x = e.getX();
         last_y = e.getY();
      }
   }

   public void mouseReleased(MouseEvent e) { }
   public void mouseClicked(MouseEvent e) { }
   public void mouseEntered(MouseEvent e) { }
   public void mouseExited(MouseEvent e) { }
    
/******************************
    Mouse Motion Event Handling
*******************************/

    /**
     * Standard method to respond MouseDragged events.
     * 
     * @param e the MouseEvent object
     *
     */
   public void mouseDragged(MouseEvent e)
   {
      //process the close box event
      int x = e.getX();
      int y = e.getY();

      if (!e.isMetaDown()) { //don't process right button drag
         Graphics g = getGraphics();
         g.drawLine(last_x, last_y, x, y);
         last_x = x;
         last_y = y;
         g.dispose();
      }
   }

   public void mouseMoved(MouseEvent e) { }

}