简体   繁体   English

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

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

I am making my chess game with javax.swing. 我正在用javax.swing制作我的国际象棋游戏。 I am using gridLayout(8,8) filled with JButtons, with background color set to brown and lightBrown as usual on chess boards. 我正在使用填充了JButtons的gridLayout(8,8),背景颜色设置为棕色和lightBrown像往常一样在棋盘上。 Now I want to put ImageIcon (king , rock , ect..) on that Buttons which I got from google images and edit them in paint.net . 现在我想将ImageIcon(国王,摇滚,等等)放在我从谷歌图片获得的按钮上,并在paint.net中编辑它们。

white king on gray background 灰色背景的白国王

But most of the pieces can move from gray buttons to light gray buttons. 但大多数碎片可以从灰色按钮移动到浅灰色按钮。 so I can either make all pieces on light gray background 所以我可以在浅灰色背景上制作所有作品

white king on light gray background 浅灰色的背景上的白王

and just swich ImageIcon depending which JButton piece is landing on (but I would rather not), Or make background color on that image transparent, but I have no idea how to do that (for example is there some color which swing making transparent automaticly) 并且只是swich ImageIcon取决于哪个JButton片落在哪个(但我宁愿没有),或者使该图像上的背景颜色透明,但我不知道如何做到这一点(例如是有一些颜色可以自动透明摆动)

Thank you for help. 谢谢你的帮助。

You should take a look to RGBA color model . 你应该看看RGBA颜色模型

In this model the A stands for alpha channel, which is generally used as an opacity channel. 在此模型中,A代表alpha通道,通常用作不透明通道。

This means that you can have a "transparent" color by setting the alpha value of your color to 0. 这意味着您可以通过将颜色的Alpha值设置为0来获得“透明”颜色。

The java.awt.Color class provides some constructors where you can specify the alpha value of your Color, for example : java.awt.Color类提供了一些构造函数,您可以在其中指定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). Color(int r,int g,int b,int a)使用指定的红色,绿色,蓝色和alpha值(范围为0 - 255)创建sRGB颜色。

You can make the background color of your image transparent by yourself, if you can't find a program which gave you this option. 如果您找不到提供此选项的程序,您可以自己使图像的背景颜色透明。

For example this code i wrote tries to remove the background colour from your "white king on gray background" image. 例如,我写的这段代码试图从你的“灰色背景上的白王”图像中删除背景颜色。 If you try to compile and run, you should get this result: 如果您尝试编译并运行,您应该得到以下结果:

测试截图

As you can see not all the background has been removed from your image, this is due to the fact that the background is made by different colours. 如您所见,并非所有背景都已从图像中删除,这是因为背景是由不同的颜色制成的。

But this example shows you that you can manipulate your images pixels in order to obtain transparency. 但是此示例显示您可以操纵图像像素以获得透明度。

I think that the best option would be to search online some chess images that have already a transparent background. 我认为最好的选择是在网上搜索一些已经具有透明背景的国际象棋图像。

For example, i can post some links here (i don't know if there are some copyright issues, take care of this), you could easily get all the images if you check URLs : 例如,我可以在这里发布一些链接(我不知道是否存在一些版权问题,请注意这一点),如果您检查URL,您可以轻松获取所有图像:

Black Rook 黑鲁克

White Queen 白皇后

Example Code : 示例代码:

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