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() {
      addMouseListener(this);
    }

    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
  }
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact