JPanel does not preserve color alpha when changing background
Two things popout
- Swing does not support alpha-based colors, or the Swing component is opaque or transparent. You have to fake it by making the component transparent and then override
paintComponent
and useAlphaCompositeto
fill, otherwise Swing won't know it should draw under your component and you'll end up with a lot of paint questions - In yours,
TranslucentPanel
you let the component paint its background and then fill it with semi-transparent versions by doubling. You must make this component transparent.
The first thing I would like to do is change TranslucentPane
so that you can control the transparency level, for example
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();
}
}
Then I would change panel_Bottom
to use it ...
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);
}
I would also, HIGHLY, recommend that you stop using layouts null
and learn how to use the appropriate layout managers, they will make your life easier.
See Stacking Components in a Container for more details.
source to share
You have an error in GUI-Button 50 line - your setter for your background is a defect due to a typo.
So, if you want to set your background to a new color, nothing happens.
But I think your transparency problem comes from line 199 in GuiSettings - there you set a composite value that makes your pixels "dark" after each call. (the reason is the call to the output method with the parameter 0.85f)
Hope I can help you
source to share
The button is still very slowly losing all transparency.
View Background with transparency for possible problems and solutions.
Basically, you need to make sure that the background of the parent component is painted first before drawing the transparent background, otherwise you will run into the problem you are describing.
source to share