mushb
asked on
how to get mouse xy coords of panel and not component on panel in awt
Hi,
I would like to know how to get the xy coords of the cursor in a mousedragged method for the panel that the component is on. At the moment i get the xy coords within the component. My objective to get the panel coords so that i can move the component to the location that the mouse is dragged to.
Thanks.
I would like to know how to get the xy coords of the cursor in a mousedragged method for the panel that the component is on. At the moment i get the xy coords within the component. My objective to get the panel coords so that i can move the component to the location that the mouse is dragged to.
Thanks.
http://forum.java.sun.com/thread.jspa?threadID=495157&messageID=2333711
ASKER
I checked the link. it doesn't tell me how to get the xy coords of the panel. i have already created the componenets on a null layout and that's ok. what i need to know is how do i get the components xy coords on the main content panel it is on so that i can allow a user to drag the component to a new location using the mouse dragged method.
Point p = SwingUtilities.convertPoin t((Compone nt)event.g etSource() , event.getPoint(), panel);
ASKER
can i use this with awt components. do i just need to add a new library.
SwingUtilities is part of Swing, but you can use it with AWT
ASKER
so if i just add the swing library it should be ok. my applet is compatible with the MS JVM. is there any partcular version of the Swing library i should use. Thanks.
i wouldn't add the swing library just to do that, easier to do the maths yourself.
Something like:
Point p = new Point(event.getX(), event.getY());
Component c = (Component) event.getSource();
Point s = c.getLocationOnScreen();
p.translate(s.x, s.y);
s = panel.getLocationOnScreen( );
p.translate(-s.x, -s.y);
Something like:
Point p = new Point(event.getX(), event.getY());
Component c = (Component) event.getSource();
Point s = c.getLocationOnScreen();
p.translate(s.x, s.y);
s = panel.getLocationOnScreen(
p.translate(-s.x, -s.y);
ASKER
will this get the location of the component relative to the Panel it's been added to. Thanks. in advance. i'll try it and if it works give you the points.
yes, panel is the component it will return the coords relative to.
ASKER
it doesn't seem to return the correct coords. i placed your code inside a mousedragged method and am printing th s.x and s.y values. however they stay the same regardless of whichever component i click on.
the screen coords of a component would stay the same if you haven't moved it.
ASKER
every compoenent laid out on the screen is return the same value for s.x and s.y. what i'm trying to do is drag a component across the panel to a new screen. i have an awt form builder applet and am trying to add code that will allow componenets which have been added to a null layout to be positioned specifically using a mouse. can you help.
which s are you referring to? It is used for the screen coords of two different components
perhaps post the code you are using
perhaps post the code you are using
ASKER
the s in your sample code, it's the Point object. i have too much code to post. if you have any code that shows 2 or more components on a panel that can be dragged using the mouse i would be grateful.
s is reused for the screen coordinates of both source and destination components
ASKER
i have a mousedragged method in the Panel as well so that i can check the xy coords as i drag the mouse over the component and then onto the panel where the compnenet ends, they are not in sync. the coords returned by the mousedragged in the panel are very diiferent to those right next to the edge of the componenet.
the corrds in the mouse event are relative to the event source's origin.
the code above will allow you to transform them to be relative to whatever compnent you wish.
the code above will allow you to transform them to be relative to whatever compnent you wish.
ASKER
how do i ensure i get the components location relative to the panel when i click on the component. this way i am able to move componenets to specific locations on the panel when i drag.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i tried it and it does not print the same xy values for the cursor position in the class that represents the componenet as it does in the class that represents the drawing area.
can you post just the relevant parts of your code