Get X and Y of a click on an ImageIcon, Java
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.
}
});
精彩评论