is it possible to add mouse listener to an image for game development. Or to any class which puts an image to a JPanel.
Asked
Active
Viewed 75 times
-2
-
add event to panel – Madhawa Priyashantha Jul 28 '16 at 14:01
-
Welcome to stack overflow :-) Please look at [ask] and how to create a [mcve]. This will help to get useful answers. – JimHawkins Jul 28 '16 at 14:20
-
then please tell me how can I specify the position of the label on the JPanel? – Asif Ahmed Jul 28 '16 at 14:32
1 Answers
2
you could use an JButton
with an BufferdImage for this, there you have the Standard listener for a JButton
to work with.
Sample:
JButton button;
BufferedImage buttonIcon;
JFrame frame = new JFrame();
try {
buttonIcon = ImageIO.read(new File("path")); //path of image
button = new JButton(new ImageIcon(buttonIcon));
} catch (IOException e) {
button = new JButton(); //couldn't load Image
}
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);

mayha
- 603
- 6
- 16