我正在为 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();
}
});
}
白色按钮是正确的效果,但绿色按钮的阴影是错误的
qq_笑_17
相关分类