• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting Relative Mouse Coordinates  RSS feed

 
Brandon Tom
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I have a 400 x 400 JPanel in the middle of a 500 x 500 JFrame. The JPanel has string drawn to it that prints out the mouse coordinates. If my mouse were placed exactly in the middle of the JFrame, the string should read (200, 200). What is the best way to accomplish this?

I've used java.awt.MouseInfo but that has only helped me get the position of the mouse relative to the entire screen.
[ March 23, 2006: Message edited by: Brandon Tom ]
 
Sam Codean
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a MouseListener to the JPanel. Implements the MouseEntered method. This will have a parameter for the Mouse Event from which you will get the Mouse Co ordinates relative to the JPanel.
hope that helps, Please get back to me in case you have doubts
 
Brandon Tom
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I needed to implement the MouseMotionListener and provide an implementation for mouseMoved (). Thanks anyway!
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!