This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes To get the coordinates of mouse pointer... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "To get the coordinates of mouse pointer..." Watch "To get the coordinates of mouse pointer..." New topic
Author

To get the coordinates of mouse pointer...

sumukha kris
Greenhorn

Joined: Nov 02, 2007
Posts: 18
Hello,

how can i get the coordintes value of the mouse pointer within the frame . I need this to draw a rectangle , when the user clicks on one of the position and drags it a rectangle has to be drawn . The starting point of the rectangle should be pixel positions of the mouse pointer when the user clicks and ending point should be pixel co ordinates when the user releases the mouse button. Can anybody help me with this.

Thanks in anticipation,
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

You need to add mouseListener and mouseMotionListener to the frame. The mouse event has getX() and getY() methods which will give you the co-ordinates you want.
You might also want to read this tutorial.

Also, the MouseInputAdapter will be helpful to you so that you need to implement only the methods you really require.

Best of luck.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: To get the coordinates of mouse pointer...
 
Similar Threads
Mouse click on label
Adding an image to a JTree node
grab pixel in java Swing
repaint() problem
Trying to implement autoscrolling on a jpanel that has a jscrollpane.