繁体   English   中英

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

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

我正在用javax.swing制作我的国际象棋游戏。 我正在使用填充了JButtons的gridLayout(8,8),背景颜色设置为棕色和lightBrown像往常一样在棋盘上。 现在我想将ImageIcon(国王,摇滚,等等)放在我从谷歌图片获得的按钮上,并在paint.net中编辑它们。

灰色背景的白国王

但大多数碎片可以从灰色按钮移动到浅灰色按钮。 所以我可以在浅灰色背景上制作所有作品

浅灰色的背景上的白王

并且只是swich ImageIcon取决于哪个JButton片落在哪个(但我宁愿没有),或者使该图像上的背景颜色透明,但我不知道如何做到这一点(例如是有一些颜色可以自动透明摆动)

谢谢你的帮助。

你应该看看RGBA颜色模型

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

这意味着您可以通过将颜色的Alpha值设置为0来获得“透明”颜色。

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

Color(int r,int g,int b,int a)使用指定的红色,绿色,蓝色和alpha值(范围为0 - 255)创建sRGB颜色。

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

例如,我写的这段代码试图从你的“灰色背景上的白王”图像中删除背景颜色。 如果您尝试编译并运行,您应该得到以下结果:

测试截图

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

但是此示例显示您可以操纵图像像素以获得透明度。

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

例如,我可以在这里发布一些链接(我不知道是否存在一些版权问题,请注意这一点),如果您检查URL,您可以轻松获取所有图像:

黑鲁克

白皇后

示例代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM