The moose likes Swing / AWT / SWT and the fly likes Getting image location Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting image location" Watch "Getting image location" New topic

Getting image location

Sunit Naik

Joined: Mar 18, 2007
Posts: 7
Hi! ,

I am adding a image to a frame, and i need to capture mouseclick events on that image. What is the best way of doing this ?

What i am trying to do is somehow get the current location of the image, and then compare the mouse click coordinates to see if they lie within the image co ordinates. How can i do this ?

Thanks for your suggestions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 39076
I presume you are familiar with the Listeners for Mouse and Mouse Motion?
You should be able to use them. You should also be able to get information from the Image about its size and position and thence work out whether the click location is within the bounds of the Image.

Sunit Naik

Joined: Mar 18, 2007
Posts: 7
I am in a Dilemma,

as I mentioned I want to capture clicks on the image, so that I can start rotating it. Initially I implemented it like this

ImageIcon img = new ImageIcon ("c:\\a.jpg");
labl = new JLabel(img);

this way I can always call getBounds() on the JLable and get its coordinates and then compare it with those which I get with the mousePressed MouseEvent. But the problem with this implementation is that later when I want to rotate the image, the method drawImage() takes in a parameter of type image  and my JLable trick fails.

graphics2d.drawImage(image, x, y, this)

If I create the image as of type Image the rotation goes well, but I can�t get the coordinates of the image (though I am able to get the height and width).

Please help me out,

1. How do I get the coordinates of an Image
2. If I use JLable and ImageIcon for storing images, how do I rotate them.

Please help me out
Thanks a lot
GeeCON Prague 2014
subject: Getting image location