// this code is Listing 12.5, pages 434-435 from your textbook
// Liang, Introduction to Java Programming

import java.awt.*;
import java.awt.color.*;
import javax.swing.*;
import java.awt.event.*;

public class ScribbleDemo extends JFrame {
	
 
    public ScribbleDemo() {
		
	getContentPane().add(new ScribblePanel()); 
    }

    public static void main(String[] args)	{
	ScribbleDemo frame = new ScribbleDemo();
	frame.setTitle("ScribbleDemo");
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setSize(300, 300);
	frame.setVisible(true);
    }

    class ScribblePanel extends JPanel 
	implements MouseListener, MouseMotionListener {
	final int CIRCLESIZE=20;
	private Point lineStart = new Point(0, 0); 
	private Graphics g;
			
	public ScribblePanel() {
	    addMouseListener(this); //mousePressed,Released,
 	                            //Clicked,Entered,Exited
	    addMouseMotionListener(this); //mouseDragged,Moved
	}
		
	// for MouseListener events
	public void mouseClicked(MouseEvent e) {	}
	public void mouseEntered(MouseEvent e) {	}
	public void mouseExited(MouseEvent e) {	}
	public void mouseReleased(MouseEvent e) {	}
	public void mousePressed(MouseEvent e) {
	    lineStart.move(e.getX(), e.getY());
	}
		
	// for MouseMotion events
	public void mouseDragged(MouseEvent e) {
	    g = getGraphics();
	    if (e.isMetaDown())  {// right click 
		g.setColor(getBackground());
		g.fillOval(e.getX() - (CIRCLESIZE / 2), 
			   e.getY() - (CIRCLESIZE /2), CIRCLESIZE, CIRCLESIZE);
	    }
	    else { // left mouse click
		g.setColor(Color.BLACK);
		g.drawLine(lineStart.x, lineStart.y, e.getX(), e.getY());
	    }
	    lineStart.move(e.getX(), e.getY());		
	    // dispose
	    g.dispose();
	}
	
	public void mouseMoved(MouseEvent e) {	}
    }
	
}
