How to maintain transparency for JButtons (java)

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

the buttons look like this so only the outside has to become transparent againI tried to use .setOpaque

but it didn't work. i can't think of any other method to get rid of the white parts. I've looked at stack overflow, but none of the methods seem to help. who has an idea?

Thank!

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);
        }

        }

      

+3


source to share


1 answer


Remove some of the default rendering properties JButton

including

  • contentAreaFilled

  • borderPainted

  • focusPainted

This will make the button smaller, well, nothing. JButton

already supports drawing icons (and can do so to verify states), so there is no need to override its method paintComponent

...

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 ImageIcon

(or other methods), mainly because it will throw IOException

if something goes wrong (rather, it doesn't work silently) and won't return until the image is loaded (and will support performance feedback if you do it right)

See Reading / Loading Image for more details

+6


source







All Articles