Adding a mouse listener to image or image Icon in java

前端 未结 3 411

How can I to add mouse listener to image or imageIcon in Java?

Here is my code. I want to do anything with imageIcon after mouse click on it

3条回答
  •  借酒劲吻你
    2020-12-20 06:38

    The simplest solution would be to use a JLabel and set it's icon property. See How to use labels for more details.

    If you must paint the image yourself (ie, you want to apply effects or perform animation across a container), then you need to add the MouseListener to the container which is rendering the image and test to see if the mouse event occurred within the context of the image based on it's location and size.

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class ImageMouseListener {
    
        public static void main(String[] args) {
            new ImageMouseListener();
        }
    
        public ImageMouseListener() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new ImagePane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class ImagePane extends JPanel {
    
            private BufferedImage img;
            private Point imgPoint;
    
            public ImagePane() {
                try {
                    img = ImageIO.read(...);
                    imgPoint = new Point(100, 100);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
    
                addMouseListener(new MouseAdapter() {
    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if (img != null && imgPoint != null) {
                            Point me = e.getPoint();
                            Rectangle bounds = new Rectangle(imgPoint, new Dimension(img.getWidth(), img.getHeight()));
                            if (bounds.contains(me)) {
                                System.out.println("I was clicked!");
                            }
                        }
                    }
    
                });
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (img != null && imgPoint != null) {
                    g.drawImage(img, imgPoint.x, imgPoint.y, this);
                }
            }
    
        }
    
    }
    

    Take a look at Performing Custom Painting and How to use Mouse Listeners for more details

提交回复
热议问题