简体   繁体   English

如何使用 SynthLookAndFeel 更改“禁用图标”样式?

[英]How can I change the “disabled icon” style using SynthLookAndFeel?

I'm currently designing a Swing app with a custom "yellow on black" Look and Feel, and it seems SynthLookAndFeel is the way to go.我目前正在设计一个 Swing 应用程序,它具有自定义的“黑底黄”外观和感觉,似乎 SynthLookAndFeel 是 go 的方式。

All my buttons consist of an ImageIcon (no text), for example:我所有的按钮都包含一个 ImageIcon(无文本),例如: 原来的

When buttons are disabled, I would like the icons to turn to a faded yellow:当按钮被禁用时,我希望图标变成淡黄色: 期望的

However, the default disabled icons are just greyscale versions of the "enabled" ones, and that breaks the yellow feel:但是,默认禁用的图标只是“启用”图标的灰度版本,这打破了黄色的感觉: 当前的

I read in this answer that disabled icons are generated internally by getDisabledIcon() , but I find no place to control it from synth's XML file.我在这个答案中读到禁用图标是由getDisabledIcon()内部生成的,但我找不到从合成器的 XML 文件中控制它的地方。

I also read about SynthLookAndFeel's SynthPainter class , but it doesn't seem to address the question of disabled icons.我还阅读了有关 SynthLookAndFeel 的SynthPainter class的信息,但它似乎没有解决禁用图标的问题。

Is there a way to control that "getDisabledIcon" behaviour using SynthLookAndFeel, or am I asking too much?有没有办法使用 SynthLookAndFeel 控制“getDisabledIcon”行为,还是我要求太多? In the latter case, what would be the best suited look and feel to use or extend for easy definition of button backgrounds, shapes, etc?在后一种情况下,最适合使用或扩展以轻松定义按钮背景、形状等的外观和感觉是什么?

Any hint is welcome.欢迎任何提示。

OK, I think I found a clean way.好的,我想我找到了一个干净的方法。

I was hesitating between finding a way with SynthLookAndFeel or subclassing another L&F... But didn't think of subclassing SynthLookAndFeel itself:-)我在寻找一种使用 SynthLookAndFeel 的方法或子类化另一个 L&F 之间犹豫不决……但没有想到子类化 SynthLookAndFeel 本身:-)

I've now got an implementation of SynthLookAndFeel that does exactly what I want, meaning the "disabled" icon is not a greyscale one, it's a desaturated, dimmed, color version:我现在有一个 SynthLookAndFeel 的实现,它完全符合我的要求,这意味着“禁用”图标不是灰度图标,它是一个去饱和、变暗的彩色版本: 在此处输入图像描述

Here we go for the full code:这里我们 go 获取完整代码:

import javax.swing.*;
import javax.swing.plaf.synth.SynthLookAndFeel;
import java.awt.*;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;

public class MySynthLookAndFeel extends SynthLookAndFeel {
    @Override
    public Icon getDisabledIcon(JComponent component, Icon icon) {
        if (icon instanceof ImageIcon) {
            return new ImageIcon(createDisabledImage(((ImageIcon)icon).getImage()));
        }
        return null;
    }

    private static Image createDisabledImage(Image i) {
        ImageProducer prod = new FilteredImageSource(i.getSource(), new RGBImageFilter() {
            public int filterRGB(int x, int y, int rgb) {
                // extract alpha mask
                int alphamask = rgb & 0xFF000000;

                // convert to HSB
                float[] hsb = Color.RGBtoHSB((rgb >> 16) & 0xff, (rgb >> 8) & 0xff, rgb & 0xff, null);
                // desaturate (half saturation)
                hsb[1] *= 0.5;
                // dim (half brightness)
                hsb[2] *= 0.5;
                // convert back to RGB
                int rgbval = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);

                // reapply alpha
                rgbval = rgbval & 0x00FFFFFF | alphamask;
                return rgbval;
            }
        });
        return Toolkit.getDefaultToolkit().createImage(prod);
    }
}

Much simpler than I thought, in the end.最后,比我想象的要简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何更改程序图标 - how can I change the programs icon 如何更改进度条对话框中的信息图标? - How can i change the information icon in progressbardialog? 如何更改JComboBox中的箭头样式 - How can I change the arrow style in a JComboBox 如何使用Java程序更改Windows中文件夹的颜色/图标? - How can I change the color/icon of folders in windows using java program? 如何在XML文件中使用SynthLookAndFeel,其中xml文件路径将使用getResourceAsStream方法加载文件? - How to use SynthLookAndFeel with xml file, where xml file path will load the file using getResourceAsStream method? 如何使用netbeans更改mac上java应用程序的默认图标? - How can I change the default icon of a java application on a mac using netbeans? 如何以编程方式更改菜单项图标? - How can i change Menu Item icon programmatically? 如何覆盖抽屉导航按钮的行为并更改图标? - How can i override drawer navigation button behaviour and change the icon? 如何在模块化 java 应用程序中使用 SynthLookAndFeel? - How to use SynthLookAndFeel in a modular java application? ScalaFX / JavaFX:如何更改ComboBox的溢出样式? - ScalaFX/JavaFX: How can I change the overrun style of a ComboBox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM