MouseMotionAdapter Example Java


MouseMotionListener is replaced by MouseMotionAdapter to make mouse event handling code easier and readable. Example given with Screenshot in Simple terms. Go on practice the program.

Adapters were introduced from JDK 1.1 to make event handling code easier. Adapters replace listener interfaces. The advantage of adapters over listeners is adapters allow the Programmer to override even one or two abstract methods defined in the corresponding listeners. Adapters are very clearly explained in Java AWT Adapters.

In the following example, MouseMotionAdapter is used instead of MouseMotionListener. MouseMotionAdapter is used to handle the mouse events when the mouse is in motion. We know earlier, when the mouse is stable, the mouse events are handled by MouseListener.

Two methods exist in MouseMotionAdapter – mouseMoved() and mouseDragged(). Let us see what Java API says about these methods.

  1. void mouseMoved(MouseEvent e): Invoked when the mouse button has been moved on a component (with no buttons no down).
  2. void mouseDragged(MouseEvent e): Invoked when a mouse button is pressed on a component and then dragged. Mouse drag events will continue to be delivered to the component where the first originated until the mouse button is released (regardless of whether the mouse position is within the bounds of the component).

Following example on MouseMotionAdapter, overrides only mouseMoved() method. The code is simple, when the mouse is moved over the frame, a message is displayed at DOS prompt along with changing the background of frame to pink color.

import java.awt.*;
import java.awt.event.*;

public class FrameMouseMotionAdapterDemo extends Frame
{
  public FrameMouseMotionAdapterDemo()
  {
    HelpMe hm = new HelpMe(this);
    addMouseMotionListener(hm);

    setSize(300, 300);
    setVisible(true);
  }
  public static void main(String args[])
  {
    new FrameMouseMotionAdapterDemo();
  }
}

class HelpMe extends MouseMotionAdapter
{
  FrameMouseMotionAdapterDemo fmmad2;
  public HelpMe(FrameMouseMotionAdapterDemo fmmad1)
  {
     fmmad2 = fmmad1;
  }
  public void mouseMoved(MouseEvent e)
  {
    System.out.println("Mouse is being moved");
    fmmad2.setBackground(Color.pink);
  }
}    

HelpMe hm = new HelpMe(this);
addMouseMotionListener(hm);

Two classes exist, FrameMouseMotionAdapterDemo and HelpMe which are not involved in inheritance. To link these classes, it requires a small technique. The parameter "this" of HelpMe represents an object of FrameMouseMotionAdapterDemo.

General way of adding MouseMotionListener to frame is addMouseMotionListener(this). But here, an object of HelpMe is passed as HelpMe extends MouseMotionAdapter. This is how the frame of FrameMouseMotionAdapterDemo is linked to MouseMotionAdapter.

Observe, the HelpMe class overrides only one abstract method mouseMoved() and does not override the other method mouseDragged(). This is the advantage of MouseMotionAdapter over MouseMotionListener.

Output screen when mouse is moved over the frame window.

ima

View All for Mouse Listener/Adapter, Mouse Motion Listener/Adapter, Key Listener/Adapter

Leave a Comment

Your email address will not be published.