开发者

Get X and Y of a click on an ImageIcon, Java

开发者 https://www.devze.com 2023-02-07 11:56 出处:网络
I\'m looking to add interactivity to an image but cannot see a way off adding a mouselistener to it. I would like to get the X & Y of where whas clicked 开发者_StackOverflow社区on the image.

I'm looking to add interactivity to an image but cannot see a way off adding a mouselistener to it. I would like to get the X & Y of where whas clicked 开发者_StackOverflow社区on the image.

The Flow if the image is:

tileset = new ImageIcon("xx.png"); //ImageIcon Image that wants to be clicked
label.setIcon(tileset); // assigned to a label
panel.add(label);       //assigned to a panel
tileScrollPane = new JScrollPane(panel); // Assigned to a scrollable pane
frame.add(tileScrollPane, BorderLayout.CENTER); // then onto a JFrame


You should be able to add a MouseListener to the label:

label.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent event) {
        // Handle click - coordinates in event.
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消