Can Images respond to mouse events?

Scott Stanchfield

All you need to do is add a MouseListener to the component in which you are drawing the image.

If you're drawing the image in the applet itself, just add a MouseListener to the applet. If you're using a Canvas, add a mouse listener to the canvas.

For example:

  public class ImageCanvas extends Canvas
         implements MouseListener {

    public ImageCanvas() {

    public void paint(Graphics g) {
      // draw image

    public void mousePressed(MouseEvent e) {
      // handle mouse being pressed

    public void mouseReleased(MouseEvent e) {
      // handle mouse being released

    // other methods in MouseListener