ActionListener for an Image

Is there a way to do this? BufferedImage doesn't seem to have an addActionListener() method. Is there a way to detect mouse clicks on the image without writing a method to detect click, get mouse (x,y) and compare to where an image is?
[243 byte] By [Vagabona] at [2007-11-27 5:01:26]
# 1

You don't want an ActionListener, you want a MouseListener.

How are you displaying your image? For a mouse listener to be added, your image needs to be displayed on a Component object (or any subclass). So for instance, you can display it on a JLabel and add the mouse listener to that label.

clairec666a at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...
# 2
I'm sticking it into a JPanel.
Vagabona at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...
# 3
You can add a mouse listener to the JPanel, but this might be a problem if the image doesn't fill the whole panel.
clairec666a at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...
# 4
I only see an addTileObserver(TileObserver to) in the API.
Vagabona at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...
# 5
> I'm sticking it into a JPanel.That'll work, but a JLabel would be a little easier, since you can just set the Icon that it displays (either through the constructor or the setIcon method) instead of having to subclass JPanel and implement the display in the paintComponent
hunter9000a at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...
# 6
> I only see an addTileObserver(TileObserver to) in> the API.JPanel and JLabel both extend JComponent, so they have the same addListener methods. On the api, scroll down to the "Methods inherited from class whatever" section.
hunter9000a at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...
# 7
But how can I tell if it is on the image? Say I have a irregular shape, how do I tell if it's been clicked?
Vagabona at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...
# 8
This will be very tricky for any non-rectangular image. BufferedImage has methods such as getWidth() and getHeight(), but if the image is not rectangular, they won't help much!
clairec666a at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...
# 9

> But how can I tell if it is on the image? Say I have

> a irregular shape, how do I tell if it's been clicked?

If you set the container (JLabel or JPanel) to the size of the image, then the mouse clicks will always be on the image somewhere. If you want to know if a specific area inside the rectangular image was clicked, then you'll have to implement that yourself. If the area is a circle in the middle of the image, then do the math to see if the distance from the center of the circle to the mouse click location is <= the radius of the circle. There's no way to automatically determine where those areas in the images are.

hunter9000a at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...
# 10
So you can't tell if a point is within an Image without doing it yourself?
Vagabona at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...
# 11

> So you can't tell if a point is within an Image

> without doing it yourself?

No, an image is just a rectangle of pixels. You can tell if the click is within that rectangle, but if you want to examine shapes that those pixels make up, then you have to do that yourself. How would java know where the shapes were, let alone which one you wanted to know about?

hunter9000a at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...
# 12
By testing if the Point was transparent or not, if it was not return the Image it is on.
Vagabona at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...
# 13

> By testing if the Point was transparent or not, if it

> was not return the Image it is on.

That's actually a really good idea, I hadn't thought of that. :) You'll still have to implement it yourself, but it'll be much easier than my earlier suggestion. And make sure that your image always has a transparent background.

hunter9000a at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...
# 14

Question on how to do that, should I extend BufferedImage and write a method the grabs the pixles of the image and check if the point is in bounds and if it is nontransparent (BTW, how can you tell if it is Transparent?), or should I extend Graphics and try it that way, or just write a static method of a helper class and use it that way?

Vagabona at 2007-7-12 10:18:53 > top of Java-index,Java Essentials,New To Java...