如何在 JButton ImageIcon 中使颜色不可见(透明)

How to make a color invisible ( transparent ) in JButton ImageIcon

我正在和 javax.swing 下棋。 我正在使用 gridLayout(8,8) 填充 JButtons,背景颜色 在棋盘上像往常一样设置为棕色和浅棕色。 现在我想把 ImageIcon (king , rock , ect..) 放在我从 google 图像中得到的按钮上,并在 paint.net 中编辑它们。

white king on gray background

但大部分作品都可以从灰色按钮移动到浅灰色按钮。 所以我可以在浅灰色背景上制作所有作品

white king on light gray background

然后根据哪个 JButton 块登陆(但我宁愿不)来切换 ImageIcon, 或者使该图像上的背景颜色透明,但我不知道该怎么做(例如,是否有一些颜色可以自动透明)

感谢您的帮助。

你应该看看RGBA color model

在此模型中,A 代表 alpha 通道,通常用作不透明通道。

这意味着您可以通过将颜色的 alpha 值设置为 0 来获得 "transparent" 颜色。

java.awt.Color class 提供了一些构造函数,您可以在其中指定 Color 的 alpha 值,例如:

Color(int r, int g, int b, int a) Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).

如果找不到提供此选项的程序,您可以自己将图像的背景颜色设置为透明。

例如,我编写的这段代码试图从您的 "white king on gray background" 图像中删除背景颜色。 如果你尝试编译 运行,你应该得到这个结果:

如您所见,并非所有背景都已从您的图像中移除,这是因为背景是由不同的颜色构成的。

但是这个例子向您展示了您可以操纵图像像素以获得透明度。

我认为最好的选择是在线搜索一些已经具有透明背景的国际象棋图像。

例如,我可以post这里的一些链接(我不知道是否有一些版权问题,请注意这个),如果您检查网址,您可以轻松获得所有图像:

Black Rook

White Queen

示例代码:

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class TransparentTest
{
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    BufferedImage image = ImageIO.read(new File("KING.jpg"));
                    BufferedImage transparentImage = removeColors(image,new Color(245,222,180));
                    createAndShowGUI(image,transparentImage);
                }
                catch(IOException ex) {
                    JOptionPane.showMessageDialog(null,"Please check your file image path","Error",JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    }
    public static void createAndShowGUI(BufferedImage image,BufferedImage transparentImage) {
        JPanel pane = new JPanel(new FlowLayout(FlowLayout.CENTER,40,10));
        pane.setBackground(Color.BLUE);
        pane.add(new JLabel(new ImageIcon(image)));
        pane.add(new JLabel(new ImageIcon(transparentImage)));
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(pane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public static BufferedImage removeColors(BufferedImage image,Color... colorsBlackList) throws IOException {
        int height = image.getHeight(), width=image.getWidth();
        BufferedImage transparentImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB); 
        for(int y=0;y<height;y++) {
            for(int x=0;x<width;x++) {
                int pixel = image.getRGB(x,y);
                int red = (pixel>>16) &0xff;
                int green = (pixel>>8) &0xff;
                int blue = (pixel>>0) &0xff;
                int alpha = 255;
                // Settings opacity to 0 ("transparent color") if the pixel color is equal to a color taken from the "blacklist"
                for(Color color : colorsBlackList) {
                    if(color.getRGB() == pixel) alpha = 0;
                }
                transparentImage.setRGB(x,y,(alpha&0x0ff)<<24 | red<<16 | green<<8 | blue);
            }
        }
        return transparentImage;
    }
}