【问题标题】:How can I change the foreground color of a text based on the background it is on?如何根据文本所在的背景更改文本的前景色?
【发布时间】:2021-12-16 08:09:34
【问题描述】:

我有一个JCheckbox,这是在JPanel 中,当您选择它时,它应该在JPanel 中显示图像。问题是这样的:正如您在屏幕截图中看到的那样,JCheckbox 的文字由于图像而难以阅读。

我在想是否有某种方法可以将文本与图像进行对比,使文本的颜色与图像相反。

我知道还有其他方法可以修复它,例如将JCheckbox 设置在图像之外,但我必须更改我的程序和结构代码的设计。

例如,这是我想要的外观:

.

这是JCheckBox目前所有的代码,很简单:

        final JCheckBox INFO_IMG = new JCheckBox("Ver img");

        INFO_IMG.setFont(new Font("Dialog", 0, 12));
        INFO_IMG.setBounds(-2, 2, 78, 13);
        INFO_IMG.setOpaque(false);
        INFO_IMG.setFocusable(false);
        INFO_IMG.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent ie) {
                if (1 == ie.getStateChange()) {
                    INFO_IMG.setText("Ver info");
                    IMG.setVisible(true);
/*                  INFO_IMG.setForegound(ROBOT.getPixelColor(
                    (int)INFO_IMG.getLocationOnScreen.getX() + 12 
                    ,(int) INFO_IMG.getLocationOnScreen().getY() + 10)); 

                   This is another way that I had thought of,
                   although it does not work well,would also have to get 
                   the opposite color from the one return.
*/
              } else { 
                    INFO_IMG.setText("Ver img");
                    IMG.setVisible(false);
                }
            }
        });
        add(INFO_IMG);

【问题讨论】:

  • 你试过UIManager属性"CheckBox.foreground"吗?如果这不是重复的,请edit 你的问题包括一个minimal reproducible example 来显示你修改的方法。
  • 或者,将图像上的文本与setText("") 进行异或运算。
  • 您好,我不知道那个属性,请您详细解释一下,谢谢。哦,对不起,现在我添加更多信息。
  • 我当然没有想到那个替代方案,我会用它作为最后的手段
  • 这里有更多使用UIManager.put的例子。

标签: java swing awt jlabel


【解决方案1】:

嗯,我找到了这种方法,可惜只适用于黑白,你能想到其他方法吗?


Public Color contrast(Image image) {
    BufferedImage buffered = toBufferedImage(image);
        
    int black = 0, white = 0;
        
    for (int x = 0; x < buffered.getWidth(); x++) {
        for (int y = 0; y < buffered.getHeight(); y++) {
            if(buffered.getRGB(x, y) == Color.BLACK.getRGB()) {
                black++;
            }else {
                white++;
            }
        }
    }
        
    System.out.println("Blanco: " + white + ", Negro: " + black);
    
    return (white > black) ? Color.BLACK : Color.WHITE;
}

private BufferedImage toBufferedImage(Image image) {
    int width = image.getWidth(null);
    int height = image.getHeight(null);
    int argb = BufferedImage.TYPE_BYTE_BINARY;
        
    BufferedImage buffered = new BufferedImage(width, height, argb);
        
    Graphics2D g2 = buffered.createGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose(); 
        
    // JOptionPane.showMessageDialog(null, new ImageIcon(buffered));

    return buffered;
}

【讨论】:

    猜你喜欢
    • 2011-04-27
    • 2016-06-02
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    相关资源
    最近更新 更多