如何在java中组合两个图标?

时间:2013-06-13 13:40:46

标签: java swing icons

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

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

4 个答案:

答案 0 :(得分:6)

  • 为每个图标获取BufferedImage
  • 创建一个相同大小的BufferedImage(我们称之为combinedImage)。
  • 致电combinedImage.createGraphics()以获取Graphics2D(称之为g)实例。
  • 将非透明图像绘制到g
  • 将透明图像绘制到g
  • 弃置g
  • 使用combinedImage作为图标。

E.G。

Merged Icons

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

答案 1 :(得分:4)

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

答案 2 :(得分:3)

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

答案 3 :(得分: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);

这样做。