简体   繁体   中英

Confusion between mouseEntered() and mouseMoved()

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) ?

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.

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, eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM