How to combine two icons in java?

2020-02-13 01:52发布

问题:

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

回答1:

  • Obtain a BufferedImage for each of the icons.
  • Create a BufferedImage (let us call it combinedImage) of the same size.
  • Call combinedImage.createGraphics() to get a Graphics2D (call it g) instance.
  • Paint the non-transparent image to g.
  • Paint the transparent image to g.
  • Dispose of g.
  • Use combinedImage for the icon.

E.G.

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


回答2:

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


回答3:

See Compound Icon. You can combine Icons along the X/Y/Z axis.



回答4:

Please try this code:

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

This will do.



标签: java swing icons