ADS

Using Mousemotionlistener On Awt Frame

The following illustrates use of MouseMotionListener on AWT Frame. MouseMotionListener listens to mouse moved and dragged events which fall under MouseEvent.
 import java.awt.*;
import java.awt.event.*;
class FrameMouseMotionListener extends Frame implements MouseMotionListener
{
    public FrameMouseMotionListener()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        setTitle("MouseEvent for Frame Demo");
        setSize(400,400);
        setVisible(true);
      
        // Add MouseMotionListener to this frame
        addMouseMotionListener(this);
    }

    public void mouseMoved(MouseEvent me)
    {
        setTitle("Moved: ["+me.getX()+","+me.getY()+"]");
    }
   
    public void mouseDragged(MouseEvent me)
    {
        setTitle("Dragged: ["+me.getX()+","+me.getY()+"]");
    }
   
    public static void main(String args[])
    {
        new FrameMouseMotionListener();
    }
}

Mouse dragging is moving it in the pressing state typically used in drag and drop
me.getX(): This method gets the x co-ordinate of the pointer.
me.getY(): This method gets the y co-ordinate of the pointer.
FrameMouseMotionListener(): This contains the code illustrating MouseMotionListener with AWT Frame.
new FrameMouseMotionListener(): This statement creates object for the class.

 The following illustrates use of MouseMotionListener on AWT Frame Using MouseMotionListener on AWT Frame

Next: Using MouseWheelListener on AWT Frame
Previous: Using MouseListener for AWT Frame

Subscribe to receive free email updates:

ADS