0

I'm making a small java game for fun to practice with my GUI programming. I want to have the center of my content pane's borderLayout be an image, and I would like to put "invisible" buttons on top of the image in specific places (to be placed later, I just want to get one working for now). My issue is getting the button to actually be invisible, it seems to leave a white square where it is now. I looked around but the only things that seem to be suggested were the .setOpaque, .setContentAreaFilled, and .setBorderPainted. (game is space related, explains the names)

                galaxyButton1 = new JButton();
                galaxyButton1.setFont(starSystem);
                galaxyButton1.setBorder(BorderFactory.createEmptyBorder(25,25,25,25) );
                galaxyButton1.setOpaque(false);
                galaxyButton1.setContentAreaFilled(false);
                galaxyButton1.setBorderPainted(false);
                Color invis = new Color(Color.TRANSLUCENT);
                galaxyButton1.setForeground(invis);
                galaxyButton1.setBackground(invis);
                galaxyButton1.addActionListener( new ButtonHandler() );
                JPanel centerPanel = new JPanel();
                centerPanel.setLayout(new BorderLayout());
                JPanel buttons = new JPanel();
                buttons.setLayout(new GridLayout( 1,0,5,5 ) );
                buttons.setOpaque(false);
                buttons.add(galaxyButton1);
                centerPanel.add(buttons,BorderLayout.CENTER);
                centerImg.setLayout(new GridBagLayout());
                centerImg.add(centerPanel);
                contentPane.add(centerImg, BorderLayout.CENTER);
  • possible duplicate question: [SO](http://stackoverflow.com/questions/4585867/transparent-jbutton) – Jedi_Maseter_Sam Jun 09 '16 at 03:25
  • I tried all of the solutions in that question, they didn't work for me because I am placing the button on top of an image. (I believe that is the cause of the issue) – ExileVirtigo Jun 09 '16 at 03:30
  • Do you have a class that gets the mouse input? If so, just create a rectangle around the image, and if the [x,y] position of the mouse intersects with the rectangle, call whatever event should happen. – Jedi_Maseter_Sam Jun 09 '16 at 03:34
  • That could work... I'll try implementing that. I intend to have a lot of buttons on one image just for the record. – ExileVirtigo Jun 09 '16 at 03:37

1 Answers1

0

Here is a code outline of how to make your own button. You will need to set up a the actual mouse in some other class.

 public class InvisibleButton implements MouseListener{

    private final Rectangle rectangle;

    public InvisibleButton(int x, int y, int width, int height){
        rectangle = new Rectangle(x,y,width,height);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int x = 0; // Set this to Mouse X
        int y = 0; // Set this to Mouse Y
        if(rectangle.contains(x,y)){
            //Set Something to True or do action here
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
Jedi_Maseter_Sam
  • 753
  • 4
  • 24