Applets can listen to the mouse. To do so, they must implement the
methods defined in the interfaceMouseListener. There are five methods
that the MouseListener interface defines:
public void mouseClicked( MouseEvent event)
public void mouseEntered( MouseEvent event)
public void mouseExited( MouseEvent event)
public void mousePressed( MouseEvent event)
public void mouseReleased( MouseEvent event)
These methods are invoked externally when some event occurs.
mouseClicked gets called when the user depresses and releases the left mouse button.
mouseEntered gets called when the mouse enters the portion of the screen where the applet sits.
mouseExited gets called when the mouse leaves the portion of the screen where the applet sits.
mousePressed gets called when the user depresses the left mouse button.
mouseReleased( MouseEvent event) gets called when the user releases the left mouse button.
The class MouseEvent has two useful methods: public int
getX() and public int getY(). These return the x and
y coordinates of the mouse pointer at the time the event occured.
An applet that intends to listen to mouse events needs to include the implements keyword in its class declaration.
The following code exemplifies such a class declaration:
public class RectangleDrawer extends JApplet implements MouseListener
{
//...
}
In addition to implementing the MouseListener interface, the
Applet object must register itself as a mouse listener. The applet
accomplishes this registration by calling addMouseListener method and
passing itself as a parameter. An object refers to itself with the keyword
this. That is, the applet needs to include the method call
addMouseListener( this ). This method should be called from within the
applet's init method.
public class RectangleDrawer extends JApplet implements MouseListener
{
public void init( )
{
addMouseListener( this );
}
// ...
}
Remember that the init method is called exactly once at the beginning of execution.
In addition to the five methods that the MouseListener interface gives you,
there are two more methods that the MouseMotionListener interface defines. To
add an aditional interface to the list of interfaces you want the class to provide, you simply
separate the second interface's name in with a comma. You must also
register the class as listening to mouse motion events. Ex.
public class RectangleBrush extends JApplet implements MouseListener, MouseMotionListener
{
public void init()
{
addMouseListener( this );
addMouseMotionListener( this );
}
//...
}
The interface MouseMotionListener requires you implement two more methods
public void mouseDraged( MouseEvent event )
public void mouseMoved( MouseEvent event)
These two methods will be automatically called (externally) when a certain event occurs.
mouseDraged gets called when the user moves the mouse while holding down the left mouse button
mouseMoved gets called when the user moves the mouse (without holding down any button)
Here is an example program that does nothing, but will compile:
public class RectangleBrush extends JApplet implements MouseListener, MouseMotionListener
{
public void init()
{
addMouseListener( this );
addMouseMotionListener( this );
}
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 )
{}
public void mouseDragged( MouseEvent e )
{}
public void mouseMoved( MouseEvent e )
{}
}
In this recitation section, you will modify RectangleBrush.java so that it draws connected lines between calls to the MouseDrag event.
Call your class LinePainter
Here's an example LinePainter applet.
Here's another applet where the width of the circle you draw
depends on how fast you're moving the mouse.