Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MouseMotionListener question

 
shawn larkin
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mouseDragged(MouseEvent event)
{
int valueX = event.getX();
int valueY = event.getY();
}
getting the x and the y coordinates of the mouse seem to return a relative number example(-14, 0) instead of an absolute number(546, 873). How could I get that absolute value, and not the relative value based on the component setting the MouseMotionListener. thanks
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up the API documentation for MouseEvent:
getX() : Returns the horizontal x position of the event relative to the source component.
The coordinates are not global to your monitor screen, they are in the coordinates within the component. -14 indicates that the mouse is now 14 pixels to the left of the left side of the component for which the mouseDragged() event was fired.
 
shawn larkin
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanks for the quick reply. I know about the not global thing, I was just wondering if you knew of a solution.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll have to get the Component that launched the event (via event.getSource()), get the screen location of that component(via Component.getScreenLocation()), and add the x and y location of the event to this point.

Voila! You have to x and y location of the event on the screen!

-Nate
 
shawn larkin
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic