想要一个JLabel带有透明背景的图像

arc*_*rcy 3 java icons swing image

我将此图标用于JTable的自定义渲染器中的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;
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的结果:

在此处输入图片说明

我想做的是消除图标的白色部分;我希望它是透明的,因此可以显示JLabel的背景。我不知道为什么在这个程序中既没有洋红色也没有蓝色?如果有人愿意告诉我,我将不胜感激。但是图像上的透明背景是我要弄清楚的主要内容。

kle*_*tra 5

图像上的透明背景是主要内容

它不透明的原因是用默认颜色(非透明)显式填充了它:-)

Graphics g = bufferedImage.getGraphics();
say("" + g.getColor());
g.fillRect(0, 0, iconW, iconH);
Run Code Online (Sandbox Code Playgroud)

输出:

java.awt.Color[r=255,g=255,b=255]
Run Code Online (Sandbox Code Playgroud)

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

我不知道为什么在这个程序中既没有洋红色也没有蓝色?如果有人愿意告诉我,我将不胜感激

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

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

frame.getContentPane().setBackground(Color.blue);
// or
frame.setBackground(Color.YELLOW);
((JComponent) frame.getContentPane()).setOpaque(false);
Run Code Online (Sandbox Code Playgroud)