An Applet with Mouse Motion Events
The applet's real estate is the area within the border
Source
AnAppletWithMouseMotion.java:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AnAppletWithMouseMotion extends Applet implements MouseMotionListener {
public void init() {
addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent e) {
System.out.println("mouse is being dragged at location (" + e.getX() + ", " + e.getY() + ")");
}
public void mouseMoved(MouseEvent e) {
System.out.println("mouse is being moved at location (" + e.getX() + ", " + e.getY() + ")");
}
}
Notes
Working with Mouse Motion
- By now, you should feel comfoprtable with behavior of the above applet, even though it introduces a new Listener
interface
This Applet's Behavior
- You should be able to figure this out yourself at this point
- This is a bare bones applet, it shows of the basic functionality of mouse motion.
Things to Do
API work
- The
MouseAdapter
class
- Notice that it contains the methods for both the MouseListener
and MouseMotionListener interfaces. WHat other Listener's methods does it cover?
Playing With the Applet
Place the following applets on the same page:
- Create a rubber banding effect -- when the user drags (holds down the left button and moves) the mouse,
draw four lines (four different colors) to the current mouse position-- as the mouse moves, the lines move as well (shrinking and stretching as their
endpoints move with the new mouse position-- hence the term 'rubber-banding').
- Display a rectangle on the screen and allow the user to drag and drop the rectangle to another location