3

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

  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

4 回答 4

6
  • 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);
    }
}
于 2013-06-13T13:48:13.603 回答
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;
    }
}
于 2013-06-13T13:49:16.267 回答
3

请参阅复合图标。您可以沿 X/Y/Z 轴组合图标。

于 2013-06-13T14:54:07.280 回答
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);

这会做。

于 2013-06-13T13:46:15.923 回答