JTree设置Icon的透明度

JTree set transparency of Icon

我想通过 "Cut" 操作给出一个节点已传输到剪贴板的视觉指示。至少一个专有 OS 使用的一种直观外观是使这幅图像相同,但略微透明。

我很想知道实际上是否有可能以某种方式使用 Windoze OS (W7) 使用的图标...但如果有可能我会更感兴趣以某种方式(在渲染器中)干扰 Icon,通过某种方式干扰 Icon.paintIcon() 使用的 Graphics 对象……显然,仅针对给定节点。我不清楚 Icon 去哪里寻找它在绘制时使用的 Graphics 对象......欢迎任何启发。

以后

非常感谢 MadProgrammer。发现这种可能性是一种提取混淆的视觉效果以对其进行操作的方法:https://home.java.net/node/674913 ...它有效。将代码放在这里以防损坏 link...

public class IconTest {

    public static void main(String[] args) {
        Icon leafIcon = UIManager.getIcon("Tree.leafIcon"); 
        // ... ("Tree.closedIcon") ("Tree.openIcon")

        BufferedImage img1 = new BufferedImage(leafIcon.getIconWidth(),
                leafIcon.getIconHeight(), BufferedImage.TYPE_INT_RGB);

        Graphics g = img1.createGraphics();
        g.drawImage(((ImageIcon) leafIcon).getImage(), 0, 0, null);
        g.dispose();

        try {
            ImageIO.write(img1, "PNG", new File("leafIcon.png"));
        } catch (IOException e) {
            System.out.println("Error writing to file leafIcon" + ", e = " + e);
            System.exit(0);
        }

    }

}

然后使用 MadProgrammer 的技术以任何您喜欢的方式改变图像:改变透明度、颜色等。很棒的东西。

I'd quite like to know whether it is in fact possible somehow to use the icons used by the Windoze OS (W7)

FileSystemView#getSystemIcon 将为您提供给定 File 的 OS 图标表示,例如...

Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File("ThatImportantDoc.docx"));

I want to give a visual indication that a node has been transferred to clipboard with a "Cut" action. One intuitive look used by at least one proprietary OS is to make this the same image, but slightly transparent.

你需要把前面的Icon画到BufferedImage,它已经应用了AlphaComposite,例如

BufferedImage img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
icon.paintIcon(null, g2d, 0, 0);
g2d.dispose();

然后您需要将生成的 BufferedImage 包装在 ImageIcon 中,这样您就可以将图像作为 Icon 传递给 API 的其余部分。

JPanel panel = new JPanel();
panel.add(new JLabel(icon));
panel.add(new JLabel(new ImageIcon(img)));
JOptionPane.showMessageDialog(null, panel, "Icon", JOptionPane.PLAIN_MESSAGE);

要使其最终发挥作用,您需要提供能够支持您的功能的 TreeCellRenderer。查看 How to Use Trees 了解更多详情

只需一个调整就可以让我做我主要想做的事情:获取 UI 图像 "from the source code"。

public class IconTest {
    public static void main(String[] args) {
        // OS folder icon
//              Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File("."));
        // proprietary word processor
//              Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File("Doc1.docx"));
        // taken from PNG file
//              Icon icon = new ImageIcon( "openIcon.png" );
        // taken directly from the Java images held somewhere (?) in the code
        Icon icon = UIManager.getIcon("Tree.leafIcon"); 
//              Icon icon  = UIManager.getIcon("Tree.openIcon"); 
        // ... ("Tree.closedIcon") ("Tree.openIcon")

        BufferedImage img = new BufferedImage( icon.getIconWidth(),
                icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setComposite(AlphaComposite.SrcOver.derive( 0.5f));
        icon.paintIcon(null, g2d, 0, 0);
        g2d.dispose();              

        JPanel panel = new JPanel();
        panel.add(new JLabel(icon));
        panel.add(new JLabel(new ImageIcon(img)));
        JOptionPane.showMessageDialog(null, panel, "Icon", JOptionPane.PLAIN_MESSAGE);              

    }
}