File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

To get the coordinates of mouse pointer...

sumukha kris

Joined: Nov 02, 2007
Posts: 18

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: 10815

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!]
I agree. Here's the link:
subject: To get the coordinates of mouse pointer...
It's not a secret anymore!