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

public class MouseEvents extends Applet implements MouseListener, MouseMotionListener
{
    String msg = "";                        // I am not implementing those methods which
    int mouseX = 0, mouseY = 0;             // are not related to my question

    public void init()
    {
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public void mouseEntered(MouseEvent me)
    {
        mouseX = 0;
        mouseY = 10;
        msg = "Mouse Entered";
        repaint();
    }

    public void mouseMoved(MouseEvent me)
    {
        mouseX = me.getX();
        mouseY = me.getY();
        showStatus("Moving mouse at "+mouseX+", "+mouseY);
    }

    public void paint(Graphics g)
    {
        g.drawString(msg, mouseX, mouseY);
    }
}

Coordinates of my applet window:

Upper left corner - (0, 0)
Lower left corner - (0, 199)
Upper right corner - (349, 0)
Lower right corner - (349, 199)

What I expect:

  1. When mouse enter the applet window, a message "Mouse Entered" should be displayed at the coordinates (0, 10)

  2. When the mouse is moving, a message "Moving mouse at mouseX, mouseY" should be displayed in the status window. Where mouseX and mouseY are the current coordinates of the mouse

What is actually happening:

The message "Mouse entered" is not being displayed at the coordinates (0, 10), instead it is getting displayed at the initial coordinates from where the mouse enteres the applet window***

For example, the mouse enters the applet window from between Lower left corner and Lower right corner, say (187, 199), then the message "Mouse Entered", instead of getting displayed at (0, 10), is getting displayed at (187, 199)

My question

In spite of specifying mouseX = 0 and mouseY = 10 in mouseEntered(), why is the message "Mouse Entered" is getting displayed at the coordinates from where the mouse enters the applet window, but not at the coordinates (0, 10)?

kevin gomes
  • 1,775
  • 5
  • 22
  • 30

2 Answers2

2
public void mouseMoved(MouseEvent me)
{
    mouseX = me.getX();
    mouseY = me.getY();
    showStatus("Moving mouse at "+mouseX+", "+mouseY);
}

Your code in the mouseMoved section is being updated whenever you move the mouse. So, the reason it is displaying mouseX and mouseY not as (0,10) is because you changed the value of mouseX and mouseY in your mouseMoved method. Causing it to display coordinates at the last position of the mouse. Try creating a different variable to keep track of the position of the mouse.

Mitch
  • 21
  • 1
2

The viewing pane gets redrawn very often as you move the mouse over it.

You are overwriting the values of mouseX and mouseY here:

public void mouseMoved(MouseEvent me)
{
    mouseX = me.getX();
    mouseY = me.getY();
    showStatus("Moving mouse at "+mouseX+", "+mouseY);
}

This causes the redrawing to occur at those coordinates when you move the mouse. If you want mouseEntered to not move, you can use a local variable, e.g.

public void mouseMoved(MouseEvent me)
{
    int currentMouseX = me.getX();
    int currentMouseY = me.getY();
    showStatus("Moving mouse at "+currentMouseX+", "+currentMouseY);
}
durron597
  • 31,968
  • 17
  • 99
  • 158
  • which function is executed first, `mouseEntered` or `mouseMoved`? – kevin gomes Jul 13 '15 at 19:47
  • @kevingomes `mouseEntered` – durron597 Jul 13 '15 at 19:47
  • Why it is not happening that until `mouseEntered` completes its execution, `mouseMoved` remains suspended? – kevin gomes Jul 13 '15 at 19:49
  • @kevingomes Because both are separate tasks queued on the [Event Dispatch Thread](http://stackoverflow.com/q/7217013/1768232) – durron597 Jul 13 '15 at 19:50
  • By the time `mouseMoved` is altering the values of `mouseX` and `mouseY`, does `mouseEntered` stops or continue its execution. What I mean to say is that whether they are running simultaneously or not? – kevin gomes Jul 13 '15 at 19:54
  • @kevingomes `mouseEntered` is a **very** quick event and only runs once (until you exit the region). `mouseMoved` happens many, many times as you move the mouse around. – durron597 Jul 13 '15 at 19:55