?
Solved

how to get mouse xy coords of panel and not component on panel in awt

Posted on 2005-03-15
21
Medium Priority
?
459 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:mushb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 10
21 Comments
 

Author Comment

by:mushb
ID: 13547902
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13549834
Point p = SwingUtilities.convertPoint((Component)event.getSource(), event.getPoint(), panel);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:mushb
ID: 13549850
can i use this with awt components. do i just need to add a new library.
0
 
LVL 92

Expert Comment

by:objects
ID: 13549906
SwingUtilities is part of Swing, but you can use it with AWT
0
 

Author Comment

by:mushb
ID: 13549962
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13550134
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);

0
 

Author Comment

by:mushb
ID: 13550207
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13550269
yes, panel is the component it will return the coords relative to.
0
 

Author Comment

by:mushb
ID: 13550331
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13550358
the screen coords of a component would stay the same if you haven't moved it.
0
 

Author Comment

by:mushb
ID: 13550434
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13550450
which s are you referring to? It is used for the screen coords of two different components

perhaps post the code you are using
0
 

Author Comment

by:mushb
ID: 13550483
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13550501
s is reused for the screen coordinates of both source and destination components
0
 

Author Comment

by:mushb
ID: 13550537
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13550546
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.
0
 

Author Comment

by:mushb
ID: 13550554
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.
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 13550561
the code i posted will convert the coords from one component to another.
0
 

Author Comment

by:mushb
ID: 13555367
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13560966
can you post just the relevant parts of your code
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question