我想给出一个视觉指示,表明节点已通过“剪切”操作转移到剪贴板。至少一个专有操作系统使用的一种直观外观是使其成为相同的图像,但稍微透明。
我很想知道实际上是否有可能以某种方式使用 Windoze OS (W7) 使用的图标...但如果有可能以某种方式进行干扰(在渲染器)与图标,通过某种方式弄乱 Icon.paintIcon() 使用的 Graphics 对象......显然,只是针对给定的节点。我不清楚图标在哪里寻找它在绘制时使用的 Graphics 对象......任何启发都将受到欢迎。
稍后
非常感谢 MadProgrammer。发现这种可能性是提取模糊视觉效果以对其进行操纵的一种方式:https://home.java.net/node/674913 ... 有用。将代码放在这里以防链接损坏...
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
将为您提供给定文件
的操作系统图标表示,例如...
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了解更多详情
关于java - JTree设置Icon的透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33530029/