import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * An applet that allows you to draw free hand
 * Left  click mouse and drag to draw
 * Right click mouse to clear 
 */

public class xDraw extends JApplet 
                   implements MouseListener, MouseMotionListener 
{
    Point startPt;				//Point class available in awt  
    Point endPt;				//has x and y coordinate			
	
    public void init() { 

		xDraw tablet = new xDraw();
    }

    public xDraw () { 							//constructor

		Container panel = getContentPane();   	//get reference to the content pane

		panel.setBackground(Color.white);	

        setVisible(true);						//make window visible

        addMouseListener(this);					//add event handler for mouse click
        addMouseMotionListener(this);			//add event handler for mouse move
    }
	
    public void mousePressed(MouseEvent evt) 
    {
		int X = evt.getX();
		int Y = evt.getY();
		startPt = new Point(X, Y);
    }
	
    public void mouseDragged(MouseEvent evt) 
    {
		endPt = new Point(evt.getX(), evt.getY());

		Graphics g = getGraphics();				//get a ref to graphics obj
												//access screen device		

		g.drawLine(startPt.x, startPt.y, endPt.x, endPt.y);  //draw from start to end

        showStatus(endPt.toString());			//display x,y coord in status bar
							
		startPt = endPt;						//new starting point
    }

    public void mouseClicked(MouseEvent evt) 
    {
        int button = evt.getModifiers();	  		//which button pressed?

        if (button == MouseEvent.BUTTON1_MASK)	  	//right button. value=16 
            mouseDragged(evt);						//call mouseDragged
													//to paint a dot

        if (button == MouseEvent.BUTTON3_MASK)	  	//right button. value=4 
		{
            repaint();								//repaint - i.e. clear
	    	showStatus("Cleared");					//show "Clear" in status bar
		}
    }
    
    public void mouseReleased(MouseEvent evt) { }		//must implement
    public void mouseExited(MouseEvent evt)   { }		//even if empty
    public void mouseEntered(MouseEvent evt)  { }
    public void mouseMoved(MouseEvent evt)    { }
	
}