-1

I want to produced a sound when I clicked on one of my buffereImage. But it display me this error: The method addMouseListener(new MouseAdapter(){}) is undefined for the type BufferedImage

here are the code:

public class TestPane extends JPanel {

    public TestPane() {
        setLayout(new PropertionalLayoutManager(400, 400));
        add(new Symbol(), new PropertionalConstraint(0f, 0));
        add(new Symbol(), new PropertionalConstraint(0.67f, 0));
        //add(new Symbol(), new PropertionalConstraint(0f, 0.4675f));
        //add(new Symbol(), new PropertionalConstraint(0.67f, 0.4675f));
        add(new Drum(), new PropertionalConstraint(0.205f, 0.1f));
        add(new Drum(), new PropertionalConstraint(0.5f, 0.1f));
        add(new Drum(), new PropertionalConstraint(0f, 0.33f));
        add(new Drum(), new PropertionalConstraint(0.705f, 0.33f));


     DRUM.addMouseListener(new MouseAdapter()
     {
          public void mouseClicked(MouseEvent me) 
          {
            Sound1.Sound5.play();
          }
        }); 


    }static {

    try {
        SYMBOL = ImageIO.read(new File("HiCrash.png"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    try {
        DRUM = ImageIO.read(new File("HiTom.png"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
jat
  • 9
  • 6
  • 1
    `MouseListener` can only be added to a component that can be made visible on the screen. By default a `BufferedImage` can not be displayed on screen, without first being wrapped by some component (like `JLabel` or painted onto the surface of a `JPanel`, for example). – MadProgrammer Aug 01 '13 at 03:15
  • So how can I insert the bufferedImage to a JPanel? – jat Aug 01 '13 at 03:17
  • Take a look at my answer ;) – MadProgrammer Aug 01 '13 at 03:18
  • I have to ask: What's a propertion? – roippi Aug 01 '13 at 03:22
  • @roippi It's a custom layout manager that I wrote for jat some time ago which attempts to resize the components it has based on an initial size and there `PropertionalConstraint` (and preferred size). If you're really curious, check out http://stackoverflow.com/questions/17847816/position-image-in-any-screen-resolution – MadProgrammer Aug 01 '13 at 03:28

1 Answers1

1

MouseListener can only be added to a component that can be made visible on the screen. By default a BufferedImage can not be displayed on screen, without first being wrapped by some component (like JLabel or painted onto the surface of a JPanel, for example).

Actually, you can't add a mouse listener to anything that doesn't provide support for it...

Instead, add the MouseListener to the Drum or Symbol class

You might want to take a read through How to write a mouse listener

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366