JPanel 在更改背景时不保持颜色 alpha
JPanel not keeping color alpha when changing background
jpanel 的背景颜色会变得比以前更不透明。值得注意的是,我正在使用 jpanel 的 setBackground 方法。以下是一些您可能想要查看的代码链接。
The Gui it's in -- 查看第 158 行。
GUI 按钮第 50 行有错误 -
由于打字错误,您的 setter 背景有缺陷。
因此,如果您想将背景设置为新颜色,则不会发生任何事情。
但我认为你的透明度问题来自 GuiSettings 中的第 199 行 - 你在那里设置了一个复合值,它使你的像素在每次调用后 "darker"。 (原因是调用了derive方法
以 0.85f 作为参数)
希望能帮到你
弹出两个东西
- Swing 不支持基于 alpha 的颜色,Swing 组件是不透明的或透明的。你必须通过使组件透明然后覆盖
paintComponent
并使用 AlphaCompositeto
填充它来伪造它,否则 Swing 将不知道它应该在你的组件下绘制,你最终会得到 wi更多油漆问题
- 在您的
TranslucentPanel
中,您允许组件绘制其背景,然后再次用它的半透明版本填充它,加倍。您需要将此组件设置为透明
我要做的第一件事是更改 TranslucentPane
以便您可以控制透明度级别,例如
public class TranslucentPane extends JPanel {
private float alpha = 1f;
public TranslucentPane() {
}
public void setAlpha(float value) {
if (alpha != value) {
alpha = Math.min(Math.max(0f, value), 1f);
setOpaque(alpha == 1.0f);
repaint();
}
}
public float getAlpha() {
return alpha
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(getAlpha()));
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.dispose();
}
}
接下来,我将panel_Bottom
改为实际使用它...
private TranslucentPane panel_Bottom;
//...
panel_Bottom = new TranslucentPane();
panel_Bottom.setBorder(new LineBorder(new Color(0, 0, 0)));
if(isTransparent){
panel_Bottom.setAlpha(0.85f);
}
我也强烈建议您停止使用 null
布局并学习如何使用适当的布局管理器,它们会让您的生活更简单
查看 Laying Out Components Within a Container 了解更多详情
The button is still very slowly losing all of it's transparency.
查看 Backgrounds With Transparency 可能的问题和解决方案。
基本上你需要确保在绘制透明背景之前先绘制父组件的背景,否则你会遇到你描述的问题。
jpanel 的背景颜色会变得比以前更不透明。值得注意的是,我正在使用 jpanel 的 setBackground 方法。以下是一些您可能想要查看的代码链接。
The Gui it's in -- 查看第 158 行。
GUI 按钮第 50 行有错误 - 由于打字错误,您的 setter 背景有缺陷。
因此,如果您想将背景设置为新颜色,则不会发生任何事情。
但我认为你的透明度问题来自 GuiSettings 中的第 199 行 - 你在那里设置了一个复合值,它使你的像素在每次调用后 "darker"。 (原因是调用了derive方法 以 0.85f 作为参数)
希望能帮到你
弹出两个东西
- Swing 不支持基于 alpha 的颜色,Swing 组件是不透明的或透明的。你必须通过使组件透明然后覆盖
paintComponent
并使用AlphaCompositeto
填充它来伪造它,否则 Swing 将不知道它应该在你的组件下绘制,你最终会得到 wi更多油漆问题 - 在您的
TranslucentPanel
中,您允许组件绘制其背景,然后再次用它的半透明版本填充它,加倍。您需要将此组件设置为透明
我要做的第一件事是更改 TranslucentPane
以便您可以控制透明度级别,例如
public class TranslucentPane extends JPanel {
private float alpha = 1f;
public TranslucentPane() {
}
public void setAlpha(float value) {
if (alpha != value) {
alpha = Math.min(Math.max(0f, value), 1f);
setOpaque(alpha == 1.0f);
repaint();
}
}
public float getAlpha() {
return alpha
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(getAlpha()));
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.dispose();
}
}
接下来,我将panel_Bottom
改为实际使用它...
private TranslucentPane panel_Bottom;
//...
panel_Bottom = new TranslucentPane();
panel_Bottom.setBorder(new LineBorder(new Color(0, 0, 0)));
if(isTransparent){
panel_Bottom.setAlpha(0.85f);
}
我也强烈建议您停止使用 null
布局并学习如何使用适当的布局管理器,它们会让您的生活更简单
查看 Laying Out Components Within a Container 了解更多详情
The button is still very slowly losing all of it's transparency.
查看 Backgrounds With Transparency 可能的问题和解决方案。
基本上你需要确保在绘制透明背景之前先绘制父组件的背景,否则你会遇到你描述的问题。