我在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