Jbutton over Jbutton background image?

前端 未结 2 417
后悔当初
后悔当初 2021-01-22 01:48

Lets say I created a 2d tile map with jbuttons and then created units on top of the map is there a way to show the background of the map when the unit(also a jbutton) is on top

2条回答
  •  鱼传尺愫
    2021-01-22 02:31

    If the Topmost JButton being Translucent can solve your purpose, here is one example code, how you can do that. Simply change the AlphaComposite values i.e. 0.7f, used in my case, to whatever deemed fit for your instance of the code :

    import java.awt.AlphaComposite;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.FlowLayout;
    
    import java.awt.image.BufferedImage;
    
    import java.io.IOException;
    
    import java.net.URL;
    
    import javax.swing.*;
    
    public class TransparentButton
    {       
        private CustomButton button;
        private ImageIcon backgroundImage;
    
        private void displayGUI()
        {
            JFrame frame = new JFrame("Transparent Button");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel contentPane = new JPanel();
            contentPane.setOpaque(true);
            contentPane.setBackground(Color.BLUE);
    
            try
            {
                backgroundImage = new ImageIcon(
                        new URL("http://gagandeepbali.uk.to/" + 
                                "gaganisonline/images/404error.jpg"));
            }
            catch(IOException ioe)
            {
                ioe.printStackTrace();
            }
    
            JButton baseButton = new JButton(backgroundImage);
            baseButton.setOpaque(true);
            baseButton.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
    
            button = new CustomButton("Transparent Button");
            baseButton.add(button);
    
            contentPane.add(baseButton);
            frame.setContentPane(contentPane);
            frame.setSize(300, 300);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new TransparentButton().displayGUI();
                }
            });
        }
    }
    
    class CustomButton extends JButton
    {
        private BufferedImage buttonImage = null;
    
        public CustomButton(String title)
        {
            super(title);
            setOpaque(false);
        }
    
        @Override
        public void paint(Graphics g)
        {
            if (buttonImage == null ||
                    buttonImage.getWidth() != getWidth() ||
                        buttonImage.getHeight() != getHeight())
            {
                buttonImage = (BufferedImage) createImage(
                                    getWidth(), getHeight());                               
            }   
    
            Graphics gButton = buttonImage.getGraphics();
            gButton.setClip(g.getClip());
            super.paint(gButton);
            /*
             * Make the graphics object sent to 
             * this paint() method translucent.
             */     
            Graphics2D g2 = (Graphics2D) g;
            AlphaComposite newComposite = 
                AlphaComposite.getInstance(
                    AlphaComposite.SRC_OVER, 0.7f);
            g2.setComposite(newComposite);      
            /*
             * Copy the JButton's image to the destination
             * graphics, translucently.      
             */         
            g2.drawImage(buttonImage, 0, 0, null);          
        }
    }
    

    Here is the output of the same :

    TRANSPARENT BUTTON

提交回复
热议问题