This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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 Java 8 in Action this week in the Java 8 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

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

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!]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: To get the coordinates of mouse pointer...
Similar Threads
Adding an image to a JTree node
repaint() problem
Trying to implement autoscrolling on a jpanel that has a jscrollpane.
grab pixel in java Swing
Mouse click on label