Draw3 (other files are same as before)

MyCanvas.java  [download]

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

public class MyCanvas extends JPanel implements MouseListener { //*1 Subclass MouseListener, overload its methods
    MyCanvas () {
	    addMouseListener (this); //*2 Sign us up for our own mouse events
    }

    // This is the draw callback
    public void paintComponent (Graphics g) {
	super.paintComponent(g);
	g.drawLine (50, 50, 100, 50);
    }

    // Listener callback
    public void mousePressed (MouseEvent event) { //*3 Receive mouse event
        System.out.println ("Mouse down at " + event.getPoint().x + ", " + event.getPoint().y); //*3
    }

    // MouseListener defines all of these, so we must supply them //*4 Must provide these cause abstract class
    public void mouseReleased (MouseEvent event) {} //*4
    public void mouseClicked (MouseEvent event) {} //*4
    public void mouseEntered (MouseEvent event) {} //*4
    public void mouseExited (MouseEvent event) {} //*4
}