gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:27:13 26 4
gpt4 key购买 nike

我将此图标用于 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;
}
}

这是我的结果:

enter image description here

我想做的是去掉图标的白色部分;我希望它是透明的,这样 JLabel 的背景就会显示出来。我不知道为什么这个程序中既没有出现洋红色也没有出现蓝色;如果有人愿意告诉我这一点,我将不胜感激。但图像上的透明背景是我想弄清楚的主要问题。

最佳答案

the transparent background on the image is the main thing

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

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

输出:

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

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

I don't know why neither magenta nor blue show up in this program; if someone cares to tell me that, I'd appreciate it

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

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

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

关于java - 想要一个带有透明背景图像的 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21234220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com