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:
When mouse enter the applet window, a message
"Mouse Entered"
should be displayed at the coordinates(0, 10)
When the mouse is moving, a message
"Moving mouse at mouseX, mouseY"
should be displayed in the status window. WheremouseX
andmouseY
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)
?