Search code examples
javaswingmaterial-designlook-and-feel

Problem with color different from white for creating the shadow at the JComponent


I'm developing a new look and feel for swing and now I have a problem when I go to create a shadow in the JComponent an example when I go to create a JButton with a color different to White I have a not correct effect of shadow

This is the code like creating the shadow.

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);
        }
    }

and this is the right effect with the white color

enter image description here

and this is the wrong effect with the other color

enter image description here

How I can do generalize my method paint shadow?

This is a minimal example for this code

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);
            }
        }
    }

}

The button white is a correct effect but the button green the shadow is wrong

enter image description here


Solution

  • I want to do answer to this question, I have a raw solution to create the shadow on JButton.

    This is the code of my method

    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]);
        }
    }
    

    The my question related to coderanch