Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2005-03-15
21
Medium Priority
?
466 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

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.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

581 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