JPanel does not preserve color alpha when changing background

At this rate, the jpanel's background color will become more opaque than before. Remarkably, I am using the setBackground method for the jpanel. Here are some links to some code you might want to look at.

Custom GUI Button

Gui it in - Look at line 158.

+3


source to share


3 answers


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 use AlphaCompositeto

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

      

Example

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.

+2


source


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

0


source


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.

0


source







All Articles