我正在尝试用Java创建一个类,它将为任何给定的UI元素添加一个简单的盒子阴影。通常情况下,它会正确显示,但是当它应用于按钮时,每次鼠标进入或退出按钮时阴影都会变暗。
绘画功能如下:
public void paint(Graphics g)
{
Rectangle bounds = g.getClipBounds();
g.setClip(bounds.x + (SHADOW_OFFSET + shadowOffsetMod) - (SHADOW_SIZE + shadowSizeMod), bounds.y + (SHADOW_OFFSET + shadowOffsetMod) - (SHADOW_SIZE + shadowSizeMod),
bounds.width + 2 * (SHADOW_SIZE + shadowSizeMod), bounds.height + 2 * (SHADOW_SIZE + shadowSizeMod));
int red = UIConstants.SHADOW_COLOR.getRed();
int green = UIConstants.SHADOW_COLOR.getGreen();
int blue = UIConstants.SHADOW_COLOR.getBlue();
int alpha = UIConstants.SHADOW_COLOR.getAlpha();
for (int x = (SHADOW_OFFSET + shadowOffsetMod) - (SHADOW_SIZE + shadowSizeMod); x < bounds.width + (SHADOW_OFFSET + shadowOffsetMod) + (SHADOW_SIZE + shadowSizeMod); x++)
{
for (int y = (SHADOW_OFFSET + shadowOffsetMod) - (SHADOW_SIZE + shadowSizeMod); y < bounds.width + (SHADOW_OFFSET + shadowOffsetMod) + (SHADOW_SIZE + shadowSizeMod); y++)
{
int dx = Math.abs(x - Math.max(Math.min(x, bounds.width + (SHADOW_OFFSET + shadowOffsetMod)), (SHADOW_OFFSET + shadowOffsetMod)));
int dy = Math.abs(y - Math.max(Math.min(y, bounds.height + (SHADOW_OFFSET + shadowOffsetMod)), (SHADOW_OFFSET + shadowOffsetMod)));
float dist = (float) Math.sqrt(dx * dx + dy * dy);
dist /= (SHADOW_SIZE + shadowSizeMod);
if (dist > 1)
dist = 1;
dist = 1 - dist;
g.setColor(new Color(red, green, blue, ((float)alpha * dist) / 255));
g.fillRect(x, y, 1, 1);
}
}
g.setClip(bounds);
super.paint(g);
}
答案 0 :(得分:0)
每次鼠标进入或退出按钮时阴影都会变暗。
不确定您的代码是如何使用的。但是(假设你是使用Swing)这是使用透明背景颜色导致的问题。
Swing组件是
在你的情况下,它听起来像组件是不透明的,但背景是透明的,所以你打破规则,你有绘画问题。
查看Backgrouds With Transparency了解更多信息和一些解决方案: