FAQ 3b
Updated: 1/7/00

>>>>>> How do I respond to the Mouse?
=====================================

The Applet has several mouse functions that you can overwrite.
These functions are inherited from the Component class.
(Applet extends Container which extends Component).

public boolean mouseDown(Event e,int x,int y);
public boolean mouseDrag(Event e,int x,int y);
public boolean mouseEnter(Event e,int x,int y);
public boolean mouseExit(Event e,int x,int y);
public boolean mouseMove(Event e,int x,int y);
public boolean mouseUp(Event e,int x,int y);


Here is a sample applet that uses the mouseMove() function:

// 'MouseApplet.java' code file
import java.applet.*;
import java.awt.*;

class MouseApplet extends Applet {

	Point last_mouse;

	MouseApplet(){
		last_mouse = new Point(0,0);
	}

	public void paint(Graphics g){
		g.drawString("last mouse = "+last_mouse.x+" / "+last_mouse.y,10,20);
	}

	public boolean mouseDown(Event e,int x,int y){
		last_mouse.x = x;
		last_mouse.y = y;
		repaint();		// redraw the screen
		return false;		// default response
	}
}


As you can see, this applet will display the mouse location when you click.  
It doesn't work when you click outside the applet.

The value returned by the function indicates if the component's (MouseApplet's) 
parent should recieve the event.  The value TRUE means that your function has 
consumed the event.  The parent won't recieve the event in this case.  The value 
FALSE means that the event still exists and should move up the component tree.  
see FAQ_2d to learn more about parents and component trees.



>>>>>> Advanced Mouse Handling
==============================

The method described above is the version 1.0 method.  It still works, but has been 
deprecated (declared old, someday won't be supported).  The new method is to 
use a MouseListener.

MouseListener is an interface, not a class.  You can't extend an interface like 
you can a class.  You have to create a class, then implement the interface.
When you implement an interface, there will be some functions that you must write.
In the case of the MouseListener, there are five functions you have to include:

class MyMouseHandler implements MouseListener {
	public void mouseClicked(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mousePressed(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
}


After you have your MouseListener, you need to tell the component (in this case the applet) 
to talk to the MouseListener.  You do this with the addMouseListener() function.  You can 
add a MouseListener to any component - a button, a textfield, etc.  But it's usually only 
useful to add it to the applet.

Here is the same applet as before, but the applet itself has become the MouseListener:

// 'MouseApplet.java' code file
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

class MouseApplet extends Applet implements MouseListener {

	Point last_mouse;

	MouseApplet(){
		last_mouse = new Point(0,0);
	}

	public void init(){
		addMouseListener(this);
	}

	public void paint(Graphics g){
		g.drawString("last mouse = "+last_mouse.x+" / "+last_mouse.y,10,20);
	}

//--- mouse listener functions, must have all five ---
	public void mouseClicked(MouseEvent e){
		last_mouse = e.getPoint();
		repaint();
	}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mousePressed(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
}



If you want to respond to mouseMove() events, you need to add a MouseMotionListener.