我在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;
}
}
,这里是我的结果:
我想要做的是消除图标的白色部分是什么;我希望这是透明的,所以JLabel的背景显示。我不知道为什么既没有洋红也没有蓝色出现在这个节目中;如果有人关心告诉我,我会很感激。但图像上的透明背景是我试图找出的主要内容。
值。我不能使用你的代码; getGraphics()返回Graphics,而不是Graphics2D,而Graphics2D不具有setRenderingHint。 – arcy
对不起......您需要createGraphics来获取Graphics2d。但我不确定它会起作用,我还没试过。我仍然不明白你为什么要填补这个问题。你只是想用alpha通道绘制图像,对吧? – NeplatnyUdaj
我想将图像中的绿色三角形放在标签上,并让标签的背景通过三角形未覆盖的所有部分显示。 – arcy