I want to create an invisible clickable object over an image in Java

前端 未结 2 1924
自闭症患者
自闭症患者 2021-01-26 06:05

So I\'m creating a game in Java in which the user clicks on an image that\'s different from the rest. I\'ve already got the images for the level created, but I just want to make

2条回答
  •  时光说笑
    2021-01-26 06:33

    Why do you want an invisible object above your image?

    Anyway, to answer your question, create a JPanel located just as your image.

    JPanel yourPanel = new JPanel() Set it's location same as your image.
    Create a MouseListener on your JPanel.

    yourpPanel.addMouseListener( new MouseAdapter() {
       @Override
       public void mousePressed( MouseEvent evnt ){
          // Your code when clicked here
       }
    });
    

提交回复
热议问题