2014-01-20 41 views
2

我在JTable的自定义渲染器中将此图标用于JLabel。当表格中的行被选中时,图标背景显示为白色。想要一个具有透明背景的图像的JLabel

我用paint.net创建了一个绿色的三角形,并将其背景设置为白色,其alpha值为255.这就是我在此代码中为JLabel创建IconImage的图像;由于外部原因,我为图标使用了不同的宽度。这里展示了什么是做一个示例程序:

package spacecheck.images; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.image.BufferedImage; 
import java.util.ArrayList; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

/** 
* represents an icon used in the directory tree; handles 'expanded' and 
* 'unexpanded' directories as well as indentation representing different 
* levels. 
* @author rcook 
* 
*/ 
public class TreeIconExample 
{ 
    public static int UNEXPANDED = 1; 
    public static int EXPANDED = 2; 

    @SuppressWarnings({"unused"}) 
    private void say (String msg) { System.out.println(msg); } 

    private static ImageIcon expandedIcon = null; 
    private static ImageIcon unexpandedIcon = null; 
    private static int  iconHeight = 0; 
    private static int  iconWidth = 0; 

    private static ArrayList<ImageIcon> cachedExpandedIcons = new ArrayList<ImageIcon>(); 
    private static ArrayList<ImageIcon> cachedUnexpandedIcons = new ArrayList<ImageIcon>(); 

    static 
    { 
    expandedIcon = new ImageIcon(TreeIconExample.class.getResource("images/Expanded.GIF")); 
    unexpandedIcon = new ImageIcon(TreeIconExample.class.getResource("images/Unexpanded.GIF")); 
    iconHeight = unexpandedIcon.getIconHeight(); 
    iconWidth = unexpandedIcon.getIconWidth(); 
    } 

    public TreeIconExample() { } 

    public static void main(String ... arguments) 
    { 
    JFrame frame = new JFrame("icon test"); 
    frame.setBackground(Color.blue); 
    JLabel label = new JLabel("background test"); 
    label.setBackground(Color.magenta); 
    TreeIconExample treeIcon = new TreeIconExample(); 
    ImageIcon icon = treeIcon.getIcon(2, false); 
    label.setIcon(icon); 
    frame.add(label); 
    frame.pack(); 
    frame.setVisible(true); 
    } 

    /** 
    * return the icon for an expanded or unexpanded level 
    * @param int level of folder relative to other levels displayed; 
    * starts at 0 and increases with depth 
    * @param boolean indicates whether this level is expanded or not. 
    * @return ImageIcon appropriate for expansion flag and level. 
    */ 
    public ImageIcon getIcon(int level, boolean expanded) 
    { 
    ImageIcon result = null; 

    // generate this icon and store it in the cache before returning it. 
    ImageIcon baseIcon = unexpandedIcon; 
    if (expanded) { baseIcon = expandedIcon; } 
    int iconH = iconHeight; 
    int iconW = iconWidth*(level+1); 

    BufferedImage bufferedImage = new BufferedImage(iconW,iconH,BufferedImage.TYPE_INT_ARGB); 
    Graphics g = bufferedImage.getGraphics(); 

    g.fillRect(0, 0, iconW, iconH); 
    g.drawImage(baseIcon.getImage(), iconWidth*level, 0, null); 
    result = new ImageIcon(bufferedImage); 

    return result; 
    } 
} 

,这里是我的结果:

enter image description here

我想要做的是消除图标的白色部分是什么;我希望这是透明的,所以JLabel的背景显示。我不知道为什么既没有洋红也没有蓝色出现在这个节目中;如果有人关心告诉我,我会很感激。但图像上的透明背景是我试图找出的主要内容。

回答

1

我认为你应该使用一个呈现提示的缓冲图像的Graphics2D:

Graphics2D g = bufferedImage.createGraphics(); 
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
     RenderingHints.VALUE_ANTIALIAS_ON); 

另外,为什么你有fillRect吗?我猜想它不会透明。

编辑:尝试运行的代码后,仅删除fillRect是不足以解决白色矩形问题:

enter image description here

注:我使用的影像在GIMP创建的GIF。

+0

值。我不能使用你的代码; getGraphics()返回Graphics,而不是Graphics2D,而Graphics2D不具有setRenderingHint。 – arcy

+0

对不起......您需要createGraphics来获取Graphics2d。但我不确定它会起作用,我还没试过。我仍然不明白你为什么要填补这个问题。你只是想用alpha通道绘制图像,对吧? – NeplatnyUdaj

+0

我想将图像中的绿色三角形放在标签上,并让标签的背景通过三角形未覆盖的所有部分显示。 – arcy

1

将您的标签设置为不透明

label.setOpaque(true)。如果它不起作用。使用不带背景的PNG文件。

如果它仍然不起作用。 您可以尝试不同的方法(如制作覆盖paintComponent并绘制三角形的自定义标签类)。

+0

setOpaque(true)显示洋红色,但我仍然有白色在洋红色顶部的三角形的背景。我很乐意使用没有背景的PNG,但我不知道如何创建一个 - 为此使用了哪些工具? – arcy

+0

那么,你可以搜索谷歌(有没有PNG的)。也有一个网站,蒸发背景。 http://clippingmagic.com/ 另外,你可以使用Photoshop的。 – Ozan

4

图像上的透明背景是最主要的

它不是透明的原因是明确地用默认的颜色是不透明的:-)

加油吧
Graphics g = bufferedImage.getGraphics(); 
say("" + g.getColor()); 
g.fillRect(0, 0, iconW, iconH); 

输出:

java.awt.Color[r=255,g=255,b=255] 

所以要么不填充,要么使用完全/部分透明的颜色。

我不知道为什么洋红色和蓝色都没有出现在这个程序中;如果有人在乎告诉我,我会很感激的

  • (在以前的帖子已经回答)品红不显示,因为标签!默认为不透明:意味着其背景没有填充背景颜色
  • 蓝色未显示,因为您将其设置为由其contentPane覆盖的框架:默认情况下,后者是一个普通的JPanel,其不透明度默认为true ,这是它填充其区域与背景

要看到蓝色的,将它设置为背景色到contentPane或更改其不透明度:

frame.getContentPane().setBackground(Color.blue); 
// or 
frame.setBackground(Color.YELLOW); 
((JComponent) frame.getContentPane()).setOpaque(false); 
+0

图像中有两个白色部分,其中一个是由'fillRect'调用创建的。除去这个问题,我仍然在三角形周围留下了一个白色区域,我猜想是因为我在图形编辑器中创建了一个三角形背景。我曾经使用过mspaint,它没有提供透明颜色;我现在使用的是gimp,除了我无法弄清楚如何去做,我应该这样做。我尝试用白色的不透明度为0%的白色填充背景,然后导出为PNG,但仍显示为白色。有不适合评论的选项,是否需要这些选项? – arcy

0

当您使用的显卡,我想你在找什么:

Graphics g = bufferedImage.getGraphics(); 
Graphics2D g2d = (Graphics2D) graphics; 
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f)); 

你可以选择你的不透明度改变,我会寻找替代fillRect 0.3f 0和1之间