2016-10-03 78 views
3

我正在用javax.swing制作我的国际象棋游戏。 我使用填充了JButtons的gridLayout(8,8),背景颜色 像往常一样在棋盘上设置为brown和lightBrown。 现在我想把ImageIcon(king,rock,ect ..)放在那些从google图像中获得并在paint.net中编辑它们的按钮上。如何在JButton中使颜色隐形(透明)ImageIcon

white king on gray background

但大多数片可从灰色按钮移动到浅灰色的按钮。 所以既可以使所有的片上的浅灰色背景

white king on light gray background

,并根据其的JButton片降落在刚刚SWICH ImageIcon的(但我宁愿不), 或者使该图像在透明背景颜色,但我不知道该怎么做(例如是否有一些颜色可以自动变为透明)

谢谢你的帮助。

+2

你为什么不使您的图片的背景中paint.net透明? – eldo

+0

不幸的是,如果我从paint.net中的片段图像中删除背景(所以只有它自己保留的部分,其余部分只是黑色和白色方块),然后将它作为ImageIcon在按钮上进行加载,它将具有白色背景透明 – Fredegar

+2

@eldo概述了最佳方法。在专用的绘画应用程序中将纯色BG更改为透明。 *“是否有一些颜色可以自动变为透明”*否。另请参阅[制作强大的可调整大小的Swing Chess GUI](http://stackoverflow.com/q/21142686/418556),以获取获取图标的不同方法棋子(在运行时产生它们)。 –

回答

2

您应该看看RGBA color model

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

这意味着你可以通过你的颜色的Alpha值设置为0

java.awt.Color类提供了一些构造函数,你可以指定一种颜色的Alpha值,例如有一个“透明”颜色:

颜色(INT R,INT克,INT b,INT a)创建的sRGB颜色与指定的红色,绿色,蓝色的 ,和α的值在范围(0 - 255)。

如果你找不到一个给你这个选项的程序,你可以让你的图像的背景颜色透明。

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

Test screenshot

正如你所看到的不是所有的背景已经从图像中删除,这是由于这样的事实的背景是由由不同的颜色。

但是这个例子说明你可以操纵你的图像像素以获得透明度。

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

例如,我可以在这里发布一些链接(我不知道是否有一些版权问题,照顾这个),你可以很容易地得到所有的图片,如果你检查的网址:

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

谢谢你的帮助很大 – Fredegar