亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 JComponent 上創建陰影的顏色不同于白色的問題

在 JComponent 上創建陰影的顏色不同于白色的問題

慕碼人8056858 2023-04-26 16:52:21
我正在為 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();            }        });    }白色按鈕是正確的效果,但綠色按鈕的陰影是錯誤的
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

我想回答這個問題,我有一個在 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]);

? ? }

}


查看完整回答
反對 回復 2023-04-26
  • 1 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號