• Post Reply Bookmark Topic Watch Topic
  • New Topic

MouseEvent.getX() and getY() somewhat inaccurate?  RSS feed

 
Matt Hoover
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using MouseListener for the first time to make a GUI of Conway's Game of Life for a school assignment. I started working on it on an XP machine, and noticed that the coordinates being returned for my clicks were about 5,25 pixels off (x,y respectively). So I added some fudge factors to my code to account for that, but eventually got it to where there was no error in x or y (or 1 pixel error at most... hard to say).

Now editing the code on a different Win7 machine, and the y-coordinate has slipped again... not by much, but enough to be bothersome (maybe 3 pixels off in y, but still 0-1 in x).

1) Why the inaccuracy?
2) Any way to fix it for all machines? My fudge factor obviously failed already with a sample set of only 2 machines.

Thanks!
 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because you added the MouseListener to the wrong component. You are probably trying to click on a panel, but you added the MouseListener to the frame. Well a frame has a border and title bar which probably acounts for the pixel difference.

Post your SSCCE that demonstrates the problem.
 
Matt Hoover
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Probably because you added the MouseListener to the wrong component. You are probably trying to click on a panel, but you added the MouseListener to the frame. Well a frame has a border and title bar which probably acounts for the pixel difference.

Post your SSCCE that demonstrates the problem.


You are 100% correct, and when making my frame the listener I remember thinking the panel was a more logical choice, but I ignored that little voice in the back of my head and forgot about it. Thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!