我想知道 Swing 中是否有某种方法可以将 ImageIcon 转换为灰度,例如:
component.setIcon(greyed(imageIcon));
我想知道 Swing 中是否有某种方法可以将 ImageIcon 转换为灰度,例如:
component.setIcon(greyed(imageIcon));
一个限制GrayFilter.createDisabledImage()
是它旨在为各种外观实现中的图标创建禁用外观。使用此ColorConvertOp
示例,以下图像对比效果:
GrayFilter.createDisabledImage()
:com.apple.laf.AquaLookAndFeel
ColorConvertOp#filter()
:com.apple.laf.AquaLookAndFeel
GrayFilter.createDisabledImage()
:com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
ColorConvertOp#filter()
:com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
/**
* @see https://stackoverflow.com/q/14358499/230513
* @see https://stackoverflow.com/a/12228640/230513
*/
private Icon getGray(Icon icon) {
final int w = icon.getIconWidth();
final int h = icon.getIconHeight();
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferedImage image = gc.createCompatibleImage(w, h);
Graphics2D g2d = image.createGraphics();
icon.paintIcon(null, g2d, 0, 0);
Image gray = GrayFilter.createDisabledImage(image);
return new ImageIcon(gray);
}
您可以使用以下内容:
ImageIcon icon = new ImageIcon("yourFile.gif");
Image normalImage = icon.getImage();
Image grayImage = GrayFilter.createDisabledImage(normalImage);