【问题标题】:How to combine two icons in java?如何在java中组合两个图标?
【发布时间】:2013-06-09 22:17:43
【问题描述】:

我有两个图标,一个是透明的,所以我需要添加一个图标,我必须在上面添加透明图标:

  public static void main(String[] args) {
      Icon icon = new ImageIcon("0.png");
      Icon icon1 = new ImageIcon("2.png");

      JLabel label = new JLabel();
      label.setIcon(icon);
      //label.setIcon(icon1);
      JFrame frame = new JFrame();
      frame.add(label, BorderLayout.CENTER);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setVisible(true);
  }

【问题讨论】:

    标签: java swing icons


    【解决方案1】:

    请试试这个代码:

    Icon icon = new ImageIcon("0.png");
    Icon icon1 = new ImageIcon("2.png");
    Image image1 = icon.getImage(); 
    Image image2  = icon1.getImage();
    int w = image1.width + image2.width;
    int h = Math.max(image1.height, image2.height);
    Image image = new BufferedImage(w, h,  TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    g2.drawImage(image1, 0, 0, null);
    g2.drawImage(image2, image1.width, 0, null);
    g2.dispose();
    ImageIcon newImg = new ImageIcon(image);
    

    这样就可以了。

    【讨论】:

    • int w = image1.width + image2.width; 似乎这会将图标 并排 放在一起,而我读到的问题是“直接在彼此之上”(第二个 - 顶部- 具有透明度)。
    【解决方案2】:
    • 为每个图标获取BufferedImage
    • 创建一个相同大小的BufferedImage(我们称之为combinedImage)。
    • 调用combinedImage.createGraphics() 以获取Graphics2D(称为g)实例。
    • 将不透明的图像绘制到g
    • 将透明图像绘制到g
    • 处理掉g
    • 使用combinedImage 作为图标。

    例如

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    class MergedIcons {
    
        public static void main(String[] args) throws Exception {
            URL urlBG = new URL("http://i.stack.imgur.com/gJmeJ.png");
            URL urlFG = new URL("https://i.stack.imgur.com/5v2TX.png");
            final BufferedImage imgBG = ImageIO.read(urlBG);
            final BufferedImage imgFG = ImageIO.read(urlFG);
            // For simplicity we will presume the images are of identical size
            final BufferedImage combinedImage = new BufferedImage( 
                    imgBG.getWidth(), 
                    imgBG.getHeight(), 
                    BufferedImage.TYPE_INT_ARGB );
            Graphics2D g = combinedImage.createGraphics();
            g.drawImage(imgBG,0,0,null);
            g.drawImage(imgFG,0,0,null);
            g.dispose();
            Runnable r = () -> {
                JPanel gui = new JPanel(new GridLayout(1,0,5,5));
    
                gui.add(new JLabel(new ImageIcon(imgBG)));
                gui.add(new JLabel(new ImageIcon(imgFG)));
                gui.add(new JLabel(new ImageIcon(combinedImage)));
    
                JOptionPane.showMessageDialog(null, gui);
            };
            SwingUtilities.invokeLater(r);
        }
    }
    

    【讨论】:

    • 另见@camickr 答案,这导致了一个很好的工人阶级。
    【解决方案3】:
    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;
        }
    }
    

    【讨论】:

      【解决方案4】:

      Compound Icon。您可以沿 X/Y/Z 轴组合图标。

      【讨论】:

      • LOL - 应该知道你已经准备好了一个例子。你有没有考虑在你的答案中包含那些可爱的屏幕截图(每当我提到你的消息控制台时 - 我肯定会!)?
      • 我太懒了 :) 这就是我创建博客的原因。以最小的努力回答问题。然后我可以花更多时间回答其他问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多