Well, first of all you draw all your images in (0,0), are you sure you want to do this? If you do this, you can click the point that belongs to all of your images (es, 0,0).
By the way, inside your MouseListener you have this method:
public void mouseClicked(MouseEvent e) { Point point = e.getPoint(); }
specify the coordinate of your click relative to the component that you are listening to. So you just need to check if the point you are clicking is inside the image area. You can do the following:
Rectangle imageBounds = new Rectangle(x,y,image_width, image_height); if (imageBounds.contains(point)){ //point is inside given image }
where x, y is the coordinate in which you draw your image using the drawImage method (0,0 in your case) and image_width, image_height is the size of your image.
EDIT:
There is an alternative to the solution discussed above. As suggested by Hovercraft Full Of Eels, you can do the following:
- create a jlabel for every image you want to display
- use JLabel setIcon () to specify the image that will be displayed on each label.
- Add your tags to your JPanel
- add a mouse listener to each jlabel
This approach is of great benefit: you do not have to worry about mouse coordinates, because each JLabel has a relative mouse listener. The only thing you should consider is the following:
using the Component instead of drawing your images, you cannot absolutize them, but you must use the appropriate LayoutManager to build your JLabel.
source share