Search code examples
javaswingjbuttontransparency

how to maintain transparency for JButtons (java)


i'm making a tank game. in my menu i want to use pictures as jbuttons, they are partly transparent and when they appear on screen the transparent parts become white.

the buttons look like this so only the outside has to become transparent againi tried using .setOpaque but this doesn't work. i can't think of any other method to get rid of the white parts. i've been looking all over stack overflow but none of the methods seem to help. anyone who has an idea?

Thanks!

package menu;

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    @SuppressWarnings("serial")
    public class MenuPanel extends JPanel implements ActionListener
    {

        private Button playKnop, highScoreKnop, quitKnop, HTPKnop;
        private JTextField naam;
        private Image achtergrond;
        private Tanks mainVenster;
        public static String naam_input;

        int x = 95, width = 200, height = 50;



        public MenuPanel(Tanks mainVenster) 
        {
            this.mainVenster = mainVenster;
            this.setLayout(null); 

            playKnop = new Button("/buttons/PLAY.png", 350, this);      
            highScoreKnop = new Button("/buttons/HS.png", 460, this);
            HTPKnop = new Button("/buttons/HTP.png", 515, this);
            quitKnop = new Button("/buttons/QUIT.png", 570, this);



            this.add(playKnop);
            this.add(quitKnop);
            this.add(HTPKnop);
            this.add(highScoreKnop);

            validate();

        }

        public class Button extends JButton
        {
            JButton button;
            ImageIcon buttonImage;

            String backgroundPath;
            int y;


            public Button(String backgroundPath, int y, MenuPanel menuPanel)
            {
                super();
                this.backgroundPath = backgroundPath;
                this.y = y;

                buttonImage = new                 
                       ImageIcon(PlayPanel.class.getResource(backgroundPath));
                this.setIcon(buttonImage); 
                this.setBounds(x, y, width, height);;
                this.addActionListener(menuPanel); 
            }

        }

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawImage(achtergrond, 0, 0, this.getWidth(), this.getHeight(), 
                this);
        }

        }

Solution

  • Remove some of the default rendering properties of the JButton including

    • contentAreaFilled
    • borderPainted
    • focusPainted

    Which will reduce the button to, well, nothing. JButton already supports the painting of icons (and can do so for a verity of states), so there should be no need to override it's paintComponent method...

    Button

    public class TestPane extends JPanel {
    
        public TestPane() {
            setBackground(Color.RED);
            setLayout(new GridBagLayout());
            try {
                BufferedImage img = ImageIO.read(getClass().getResource("/1xaN3.png"));
                JButton btn = new JButton(new ImageIcon(img));
                btn.setOpaque(false);
                btn.setContentAreaFilled(false);
                btn.setBorderPainted(false);
                btn.setFocusPainted(false);
    
                add(btn);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
    }
    

    Personally, I prefer to load my images using ImageIO instead of ImageIcon (or other methods), mostly because it will throw an IOException if something goes wrong (rather the failing silently) and won't return until the image is loaded (and supports progress feedback if you do it right)

    Have a look at Reading/Loading an Image for more details