gpt4 book ai didi

java - 在 JComponent 上创建阴影的颜色不同于白色的问题

转载 作者:行者123 更新时间:2023-11-30 12:04:14 25 4
gpt4 key购买 nike

我正在为 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);
}
}

这是白色的正确效果

enter image description here

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

enter image description here

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

这是此代码的最小示例

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

}

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

enter image description here

最佳答案

我想回答这个问题,我有一个在 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 有关

关于java - 在 JComponent 上创建阴影的颜色不同于白色的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57232223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com