-2

is it possible to add mouse listener to an image for game development. Or to any class which puts an image to a JPanel.

Asif Ahmed
  • 93
  • 1
  • 8

1 Answers1

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