java - 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 = ""; // not implementing methods int mousex = 0, mousey = 0; // not related 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 @ "+mousex+", "+mousey); } public void paint(graphics g) { g.drawstring(msg, mousex, mousey); } }
coordinates of applet window:
upper left corner - (0, 0)
lower left corner - (0, 199)
upper right corner - (349, 0)
lower right corner - (349, 199)
what expect:
when mouse enter applet window, message
"mouse entered"
should displayed @ coordinates(0, 10)
when mouse moving, message
"moving mouse @ mousex, mousey"
should displayed in status window.mousex
,mousey
current coordinates of mouse
what happening:
the message "mouse entered"
not being displayed @ coordinates (0, 10)
, instead getting displayed @ initial coordinates mouse enteres applet window***
for example, mouse enters applet window between lower left corner
, lower right corner
, (187, 199)
, message "mouse entered"
, instead of getting displayed @ (0, 10)
, getting displayed @ (187, 199)
my question
in spite of specifying mousex = 0
, mousey = 10
in mouseentered()
, why message "mouse entered"
getting displayed @ coordinates mouse enters applet window, not @ coordinates (0, 10)
?
the viewing pane gets redrawn move mouse on it.
you overwriting values of mousex
, mousey
here:
public void mousemoved(mouseevent me) { mousex = me.getx(); mousey = me.gety(); showstatus("moving mouse @ "+mousex+", "+mousey); }
this causes redrawing occur @ coordinates when move mouse. if want mouseentered
not move, can use local variable, e.g.
public void mousemoved(mouseevent me) { int currentmousex = me.getx(); int currentmousey = me.gety(); showstatus("moving mouse @ "+currentmousex+", "+currentmousey); }
Comments
Post a Comment