Chapter 2 - EventListeners
3. The MouseMotionListener
|
/* MouseMotionListener will enable you to performactions when the mouse moves over a hotspot. You can also use this to drag things,that will require MouseListener and MouseMotionListener at the same time. This applet will show a rectangle that will changecolor when the mouse moves over it. It's basically the same as the previous one. */ import java.awt.*;
// Tells the applet you will be using the MouseMotionListenermethods. public class MouseMotionExample extends Applet implements MouseMotionListener
int rect1xco,rect1yco,rect1width,rect1height; // wll be true when the Mouse is in theRectangle
public void init()
rect1xco = 20;
// Add the MouseMotionListener to yourapplet
public void paint(Graphics g)
g.fillRect(rect1xco,rect1yco,rect1width,rect1height); g.setColor(Color.blue); // This will show the coordinates of themouse
} /* If you use MouseMotionListener then these methodshave
to be here
// This will be excuted whenever the mousemoves
in the applet
} // This is works like mouseMoved but onlywhen
the mouse is being pressed
/* It's quite easy, but this isn't a good exampleof
programming.
}
|