How to combine two icons in java?

后端 未结 4 879
滥情空心
滥情空心 2020-12-19 18:54

I have two icons one is transparent so I need to add one icon and I have to add the transparent icon on top of that:

  public static void main(String[] args)         


        
4条回答
  •  遥遥无期
    2020-12-19 19:28

    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    
    public class MergedIcon implements Icon {
    
        private int m_iconWidth;
        private int m_iconHeight;
        private BufferedImage m_buffer;
    
        public MergedIcon(Icon backgroundImage, Icon topImage) {
            this(backgroundImage, topImage, 0, 0);
        }
    
        public MergedIcon(Image backgroundImage, Image topImage) {
            this(backgroundImage, topImage, 0, 0);
        }
    
        public MergedIcon(Icon backgroundImage, Icon topImage, int offsetX, int offsetY) {
            this(iconToImage(backgroundImage), iconToImage(topImage), offsetX, offsetY);
        }
    
        public MergedIcon(Image backgroundImage, Image topImage, int offsetX, int offsetY) {
            m_iconWidth = backgroundImage.getWidth(null);
            m_iconHeight = backgroundImage.getHeight(null);
    
            m_buffer = new BufferedImage(m_iconWidth, m_iconHeight, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = (Graphics2D) m_buffer.getGraphics();
            g.drawImage(backgroundImage, 0, 0, null);
            if (topImage != null) {
                g.drawImage(topImage, offsetX, offsetY, null);
            }
        }
    
        @Override
        public int getIconHeight() {
            return m_iconHeight;
        }
    
        @Override
        public int getIconWidth() {
            return m_iconWidth;
        }
    
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.drawImage(m_buffer, x, y, null);
        }
    
        public static Image iconToImage(Icon icon) {
            if (icon == null)
                return null;
            if (icon instanceof ImageIcon)
                return ((ImageIcon) icon).getImage();
    
            return iconToBufferedImage(icon);
        }
    
        public static BufferedImage iconToBufferedImage(Icon icon) {
            if (icon == null)
                return null;
    
            BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
            icon.paintIcon(null, image.getGraphics(), 0, 0);
            return image;
        }
    }
    

提交回复
热议问题