Example -- Applet with mouse move events

In this example, the applet implements the MouseMotionListener interface. This applet demonstrates the mouse events when the mouse is simply moved within the applet. The applet also has a Refresh button and hence implements the ActionListener interface to handle the button click event.

Java Runtime Environment is required to properly see the Applet. Please check the following link

Listing 2.1.6 shows the source code for the above program.

Move the mouse within the applet. As the mouse is moved, the events are handled by the mouseMoved method. The MouseEvent object passed to this method is used to obtain the current location of the mouse, i.e., the (x,y) coordinates of the mouse relative to the applet. A small filled rectangle is displayed at each mouse location. 

The Refresh button is used to refresh the applet. As in the previous example, the repaint method is not called within the event handling method after each mouse event occurs.

The MouseMotionListener requires the mouseDragged method also to be implemented. In this applet, we don't have anything to do when the mouse is pressed and dragged within the applet. Hence the method is declared but no code is provided.