MouseMotionAdapter Example getX() getY() Cursor Location


MouseMotionAdapter Example is a good example to integrate classes. Two classes extending Applet and MouseMotionAdapter are integrated through their constructors with Composition – "has-a" relationship.

Let us see the logic of the code. Before this, if not acquainted with adapters, go through Java AWT Adapters.

As usual applet requires a HTML file to run. Following are two classes.

MouseMotionAdapter Example

HTML File to run the applet: Demo.html



Applet File: AppletMouseMotionAdapterDemo.java

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class AppletMouseMotionAdapterDemo extends Applet
{
  int x, y;
  String str = "";

  public void init()
  {
    Helper h1 = new Helper(this);
    addMouseMotionListener(h1);
  }

  public void paint(Graphics g)
  {
    g.drawString(str + " at "+x+", " +y, x, y);
    showStatus("Observe mouse is "+str + " at "+x+", " +y);
  }
}

class Helper extends MouseMotionAdapter
{
  AppletMouseMotionAdapterDemo ammad2;
  public Helper(AppletMouseMotionAdapterDemo ammad1)
  {
    ammad2 = ammad1;
  }
  public void mouseMoved(MouseEvent e)
  {
    ammad2.setBackground(Color.yellow);
    ammad2.str = "Moved";
    ammad2.x = e.getX();
    ammad2.y = e.getY();
    ammad2.repaint();
  }
  public void mouseDragged(MouseEvent e)
  {
    ammad2.setBackground(Color.cyan);
    ammad2.str = "Dragged";
    ammad2.x = e.getX();
    ammad2.y = e.getY();
    ammad2.repaint();
  }
}

Two classes exist AppletMouseMotionAdapterDemo (extending Applet) and Helper (extending MouseMotionAdapter). They are very disjoint, in the sense, not involved in inheritance (exist as two separate .class files). Now the question is how to integrate them.

Create a object of one class in another and pass them in between and this is known as composition in Java. This is exactly happened here.

Helper h1 = new Helper(this);
addMouseMotionListener(h1);

An object of Helper class is created in init() method of AppletMouseMotionAdapterDemo class. The parameter "this" of Helper constructor refers an object of AppletMouseMotionAdapterDemo. "this" is implicitly converted into an object of AppletMouseMotionAdapterDemo by JVM as Helper constructor in the Helper class requires an object of AppletMouseMotionAdapterDemo. Observe the Helper class code.

The general way of adding a listener is addMouseMotionListener(this). Because AppletMouseMotionAdapterDemo does not extend MouseMotionAdapter and as Helper class extends MouseMotionAdapter, the object h1 of Helper class is passed to addMouseMotionListener() method. This is how AppletMouseMotionAdapterDemo and Helper classes are integrated.

Infact, the above two statements can made as one statement as follows.

addMouseMotionListener(new Helper(this));

Now let us see the other way of how Helper class gets an object of Applet extended by AppletMouseMotionAdapterDemo to make use of in its class. For example, the Helper class would like to change the background of Applet window (extended by AppletMouseMotionAdapterDemo).

AppletMouseMotionAdapterDemo ammad2;
public Helper(AppletMouseMotionAdapterDemo ammad1)
{
ammad2 = ammad1;
}

The Helper constructor gets an object of AppletMouseMotionAdapterDemo ammad1 which is passed earlier through this keyword of h1 object. ammad1 is local to Helper constructor. How ammad1 is to be made available to all the Helper class methods? Create a reference variable of AppletMouseMotionAdapterDemo ammad2 and assign with ammad1. Now ammad2 is made use of in mouseMoved() and mouseDragged() methods.

paint() method is a callback method called automatically in two cases – when frame is created and later whenever the frame is resized. If the programer would like to call the paint() method in the code, he must call repaint() but never paint(). Read more about repaint() method in Java repaint() – Call paint().


ima
Output screen when mouse is moved and mouse is dragged (MouseMotionAdapter Example)

Pass your comments on this tutorial "MouseMotionAdapter Example".

1 thought on “MouseMotionAdapter Example getX() getY() Cursor Location”

Leave a Comment

Your email address will not be published.