4

I came across this problem in a game I'm writing but have reproduced the issue on a separate jar that consists only of a frame, a panel, and a mouse motion listener.

The issue is that I draw a rectangle - for example at x:512, y:384 (48x48).

Using a mouse motion listener on the frame, it always reports the Y axis around 25 pixels less. So while the coord of the rectangle should be x:512, y:384, the mouse motion listener reports as x:512, y:409.

I theoretically could just add the difference to the mouse Y, but I need to understand why this happening.

Full code for the three example classes

helion3
  • 34,737
  • 15
  • 57
  • 100
  • Probably it reports either the tip or the bottom of the mouse. – arynaq May 28 '13 at 23:21
  • I figured that might be the case, but at least visually, there's a about 10px from the bottom of my pointer to the top of the box. – helion3 May 28 '13 at 23:22

2 Answers2

3

it always reports the Y axis around 25 pixels less

Maybe you added the MouseListener to the frame instead of the panel.

You draw your Rectangles on the panel, but the panel is located (on my OS) 30 pixels down from the top of the frame so your coordinates don't match. The X value should also be out by the width of the frame border.

Try adding the listener to the panel.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • That was the issue, I hadn't realized the frame included the top of the window as the coords, and that explains the offset. Moving the listener to the panel solved it. – helion3 May 28 '13 at 23:34
1

The origin of Frame and Board is different. The origin of Board is the (0, 25) of Frame.

Got it now?

Not a good image, though. First, screenshot won't show cursor, so I drew it. Second, I didn't put my cursor exactly on (0, 25) but just around it.

johnchen902
  • 9,531
  • 1
  • 27
  • 69