Mouse Motion Listener Event in Advanced Java Programming

Home  »  Java  »  Advance Java »    Java Event Handling »    MouseMotionListener

MouseMotionListener is another built-in interface for handling mouse events. This interface handles events that are associated with the movement of the mouse. MouseMotionListener defines two methods that are associated with the movement of a mouse. These methods are:

  1. void mouseMoved(MouseEvent me) Is invoked when you move a mouse on an AWT object that is being traced for mouse movement events.
  2. void mouseDragged(MouseEvent me) Is invoked when a mouse button is pressed and the mouse is moved on an AWT object that is being traced for mouse movement events.

The addMouseMotionListener() method is used for tracing an AWT object for the occurrence of a mouse movement event. The following program code shows how to implement the MouseMotionListener interface:

Implementing MouseMotionListener:


The above code shows how to implement the MouseMotionListener interface with the applet window. A freehand drawing mechanism is created where dragging the cursor plots pixels at those positions creating an image. The mouseDragged() method is used for retrieving and plotting the pixel positions and code will be stored in the file called MouseMotionListenerTest.java, which on compilation will crate a class file called MouseMotionListenerTest.class. Now compile this file using below commands in the DOS mode:

C:\>jdk1.4\bin>javac MouseMotionListenerTest.java
C:\>jdk1.4\bin>appletviewer MouseMotionListenerTest.java


"Output of MouseMotionListenerTest.java"


Download Complete Program

  -  



Event Listener


Action Listener Adjustment Listener Mouse Listener
Mouse Motion Listener Window Listener Key Listener
Focus Listener ITEM Listener Text Listener




No comments:

Post a Comment