Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Discrepancy between getX()/getY() & graphics

 
Art Metzer
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I am working with graphics in a JFrame. Within a graphics context, I drew a box in the vicinity of 325, 50. I want to check for mouse clicks in this box. I click on the center of the box. I would expect the MouseEvent's getX() and getY() methods to return 325 and 50, but instead they return 329 and 73!
It looks like when it comes to getX() and getY() in JFrames, at least, Java adds on the pixels at the left of the JFrame (there are only three or four) and at the top of the JFrame (the ones that make up the title bar). Are there any methods I could call to find out where the mouse is clicked that uses the same coordinate system as, say, drawRect() or drawLine()?
Thanks, as always.
Art
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you nest a JPanel inside your JFrame you will get a more predictable result.
 
Grant Crofton
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also find how many pixels it will be out by using Insets, something like this:

G.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic