【问题标题】:Problem with color different from white for creating the shadow at the JComponent在 JComponent 处创建阴影的颜色与白色不同的问题
【发布时间】:2019-07-27 12:38:41
【问题描述】:

我正在为 swing 开发一种新的外观和感觉,现在当我在 JComponent 中创建阴影时遇到问题 一个示例当我去创建颜色与白色不同的 JButton 时我有一个不正确的问题阴影效果

这是创建阴影的代码。

protected void paintShadow(@NotNull Graphics g, @NotNull JComponent c){
        int shade = 0;
        int topOpacity = 80;
        int pixels = UIManager.getInt("Button[Default].shadowPixel");
        JButton b = (JButton) c;
        for (int i = 0; i < pixels; i++) {
            g.setColor(new Color(shade, shade, shade, ((topOpacity / pixels) * i)));
            g.drawRoundRect(i, i, b.getWidth() - ((i * 2) + 1), b.getHeight() - ((i * 2) + 1), 7, 7);
        }
    }

这就是白色的正确效果

这是用其他颜色的错误效果

如何概括我的方法绘制阴影?

这是此代码的最小示例

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;

/**
 * @author https://github.com/vincenzopalazzo
 */
public class MaterialMain extends JFrame {

    static {
        UIManager.put("Button[Default].shadowPixel", 3);
    }


    public void init() {
        JPanel panel = new JPanel();


        JButton witheRightEffect = new JButton("shadow withe");
        witheRightEffect.setUI(new ShadowButtonUI());

        JButton otherColorWrongEffect = new JButton("shadow other color");
        otherColorWrongEffect.setBackground(Color.GREEN);
        otherColorWrongEffect.setUI(new ShadowButtonUI());

        panel.add(witheRightEffect);
        panel.add(otherColorWrongEffect);

        setTitle("Look and feel");

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setSize(630, 360);

        add(panel);

        setLocationRelativeTo(null);

        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MaterialMain main = new MaterialMain();
                main.init();
            }
        });
    }

    public class ShadowButtonUI extends BasicButtonUI{

        @Override
        public void installUI(JComponent c) {
            super.installUI(c);
            c.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        }

        @Override
        public void paint(Graphics g, JComponent c) {
            super.paint(g, c);
            paintShadow(g, c);
        }

        protected void paintShadow( Graphics g,  JComponent c){
            int shade = 0;
            int topOpacity = 80;
            int pixels = UIManager.getInt("Button[Default].shadowPixel");
            JButton b = (JButton) c;
            for (int i = 0; i < pixels; i++) {
                g.setColor(new Color(shade, shade, shade, ((topOpacity / pixels) * i)));
                g.drawRoundRect(i, i, b.getWidth() - ((i * 2) + 1), b.getHeight() - ((i * 2) + 1), 7, 7);
            }
        }
    }

}

按钮白色是正确的效果,但按钮绿色阴影是错误的

【问题讨论】:

  • 非常感谢您的回答,但是为了添加一个示例,我需要所有的外观和提要,所以在帖子中我添加了对 Github 项目的引用
  • “我添加了对 Github 项目的引用” 我没有关注代码链接。如果您不能将其归结为 MRE / SSCCE,那么预计会获得更少的帮助,如果有的话,以后会。
  • #vincenzopalazzo 不可能知道您在寻找什么 - 正如您所说的外观和感觉 - 所以它看起来不错!去吧!
  • 我建议使用公式 ((topOpacity / 像素) * i) 进行良好的调试 - 打印出所有值,看看你得到的是你所期望的。
  • 我添加了最小示例,非常感谢您的帮助@AndrewThompson

标签: java swing material-design look-and-feel


【解决方案1】:

我想回答这个问题,我有一个在 JButton 上创建阴影的原始解决方案。

这是我的方法的代码

public class ShadowButton {
    public static void main(String... args) {
        SwingUtilities.invokeLater(ShadowButton::new);
    }

    public ShadowButton() {
        var fadeWidth = 30;
        var p = new JPanel();
        var b = new JButton("with fading out border!!!") {
            @Override
            public void paintBorder(Graphics g) {
                var rec = g.getClip().getBounds();
                var c = this.getBackground();
                var d = this.getParent().getBackground();
                for (int i = 0; i < fadeWidth; i++) {
                    var col = mixColor(c, d, 1.0 * (i + 1) / fadeWidth);
                    g.setColor(col);
                    g.drawRect(rec.x + i, rec.y + i, rec.width - 2 * i, rec.height - 2 * i);
                }
            }
        };
        b.setFocusable(false);
        b.setBackground(Color.GREEN);
        b.setForeground(Color.BLACK);
        Font f = new Font("Arial", Font.BOLD, 36);
        b.setFont(f);
        b.setBorder(BorderFactory.createLineBorder(b.getBackground(), fadeWidth, false));
        p.setBackground(Color.RED);
        p.add(b);
        var frame = new JFrame("Shadoe Demo");
        frame.setContentPane(p);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static Color mixColor(Color c, Color d, double factor) {
        float[] cc = c.getComponents(null);
        float[] dd = d.getComponents(null);
        float[] result = new float[cc.length];
        for (int i = 0; i < 4; i++) {
            result[i] = (float) (factor * cc[i] + (1 - factor) * dd[i]);
        }
        return new Color(result[0], result[1], result[2], result[3]);
    }
}

关于coderanch的我的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 2021-05-03
    • 2011-12-19
    • 2015-07-14
    • 2018-11-30
    相关资源
    最近更新 更多