Advertisement

Swing confused about component following mouse pointer

Started by January 11, 2016 08:59 PM
3 comments, last by KenWill 8 years, 11 months ago

x = button.getX() + e.getX();
y = button.getY() + e.getY();

button.setLocation(x,y);


What i'm confused about are the first two lines. How does that work? Lets say the button x coordinates are 34 and the mouse x coordinates are 37, isn't that a total of 71 when you add it up? Then how does this work? Only setting them to the mouse's position makes the button go crazy when moving but i noticed that it goes away from the mouse position. Does e.getX() return the distance from the button coordinates(which are in the upper corner) to the mouse coordinates?

Assuming that e is a MouseEvent object, then getX and getY will return the coordinates of the mouse relative to the component that holds the mouse listener. I assume you've called addMouseListener (or addMouseMotionListener) on the parent of the button, and not the button itself? If not, that would explain why the button goes crazy :)

If this didn't help, we'll need to see some more of your code. Good luck :)
Programmer of HolyPoly games.
Advertisement

Assuming that e is a MouseEvent object, then getX and getY will return the coordinates of the mouse relative to the component that holds the mouse listener. I assume you've called addMouseListener (or addMouseMotionListener) on the parent of the button, and not the button itself? If not, that would explain why the button goes crazy :)
If this didn't help, we'll need to see some more of your code. Good luck :)

Is that the container that holds the component? Like a JPanel?
And also what does "relative to" mean? Sorry, but english is not my first langauge.

Is that the container that holds the component? Like a JPanel?
And also what does "relative to" mean? Sorry, but english is not my first langauge.



Some systems do it one way, some systems do it another way.

The mouse coordinates need to be relative to something. "Relative to" means the location where (0,0) is at.

Often they are relative to the top left corner of the screen.
[attachment=30246:screen-upper-left.PNG]

Many old systems in the 1970s and 1980s had mouse positions relative to the bottom left corner of the screen.
[attachment=30248:screen-lower-left.PNG]

Many systems can get mouse positions relative to a specific window. This is probably the corner used in your code's button.setLocation().
[attachment=30249:window-top-left.PNG]

Other times the position is relative to a specific panel
[attachment=30247:control-top-left.PNG]

Other times the position is relative to a specific button or UI control. This is probably the corner used in your code's e.getX() and e.getY().
[attachment=30250:button-relative.PNG]

Different systems choose different locations, and where the (0,0) position is centered depends on what object is listening to the event and how the system is implemented.


In the case of your code:


x = button.getX() + e.getX();
y = button.getY() + e.getY();
 
button.setLocation(x,y);

Probably e.getX and e.getY are getting the position relative to the button. This is like the final image above.

Then the programmer wants to turn it back into a position relative to the entire screen, so they add the button's screen position to the mouse position relative to the button position.

Is that the container that holds the component? Like a JPanel?
And also what does "relative to" mean? Sorry, but english is not my first langauge.



Some systems do it one way, some systems do it another way.

The mouse coordinates need to be relative to something. "Relative to" means the location where (0,0) is at.

Often they are relative to the top left corner of the screen.
attachicon.gifscreen-upper-left.PNG

Many old systems in the 1970s and 1980s had mouse positions relative to the bottom left corner of the screen.
attachicon.gifscreen-lower-left.PNG

Many systems can get mouse positions relative to a specific window. This is probably the corner used in your code's button.setLocation().
attachicon.gifwindow-top-left.PNG

Other times the position is relative to a specific panel
attachicon.gifcontrol-top-left.PNG

Other times the position is relative to a specific button or UI control. This is probably the corner used in your code's e.getX() and e.getY().
attachicon.gifbutton-relative.PNG

Different systems choose different locations, and where the (0,0) position is centered depends on what object is listening to the event and how the system is implemented.


In the case of your code:


x = button.getX() + e.getX();
y = button.getY() + e.getY();
 
button.setLocation(x,y);

Probably e.getX and e.getY are getting the position relative to the button. This is like the final image above.

Then the programmer wants to turn it back into a position relative to the entire screen, so they add the button's screen position to the mouse position relative to the button position.

Thanks a lot! That makes a lot more sense now. :3

This topic is closed to new replies.

Advertisement