简体   繁体   中英

Add MouseListener to java.awt.Image

Is there a way I can add a MouseListener to an Image? I took a look at this StackOverflow question, but it didn't really answer how to do this for a java.awt.Image , as there is no Graphics2D Image class. Or at least one I've found.


EDIT:

To clarify, let me try to explain:

With a Ellipse2D , I can say:

if(ellipse2D.contains(mouseX, mouseY) {
    ...do something
}

Is this possible with a java.awt.Image (ie image.contains() )

Also, this is how the Image gets added to the JPanel:

Image image = item.getIcon().getImage(); //item.getIcon() returns a javax.swing.ImageIcon
g.drawImage(image, imageX, imageY, null);

You can just put the image in a JLabel and add the listener to the JLabel

ImageIcon image = item.getIcon();
JLabel labelWithImage = new JLabel(image);
labelWithImage.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("Mouse Entered Over Image");
    }
});
panel.add(labelWithImage);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM