JPanel not keeping color alpha when changing background

五迷三道 提交于 2019-11-27 08:07:58

问题


Ever so slowly the jpanel's background color will become more opaque than it previously was. Notable I am using the setBackground method of the jpanel. Here are a few links to code you might want to look at.

Custom GUI Button

The Gui it's in -- Look at line 158.


回答1:


Two things popout

  1. Swing doesn't support alpha based colors, either a Swing component is opaque or transparent. You have to fake it, by making the component transparent and then override paintComponent and use a AlphaCompositeto fill it, otherwise Swing won't know it should be painting under you component and you'll end up wi a bunch more paint issues
  2. In your TranslucentPanel, you're allowing the component to paint its background and then fill it again with a translucent versions of it, doubling up. You need to set this component to transparent

The first thing I would do is change the TranslucentPane so 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();

    }

}

Next, I would change panel_Bottom to actually 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 null layouts and learn how to use appropriate layout managers, they will make your life simpler

Have a look at Laying Out Components Within a Container for more details




回答2:


You have an error in GUI-Button line 50 - your setter for your background is defect because of a typo.

So if you want to set your background to a new color, nothing is happening.

But I think your problem with transparency is coming from line 199 in GuiSettings - there you are setting a composite value which makes your pixels "darker" after each call. (the reason is the call of the derive method with 0.85f as parameter)

Hope I could help you




回答3:


The button is still very slowly losing all of it's transparency.

Check out Backgrounds With Transparency for the likely problem and solutions.

Basically you need to make sure the background of the parent component is painted first before you paint the transparent background, otherwise you experience the problem you describe.



来源:https://stackoverflow.com/questions/31105099/jpanel-not-keeping-color-alpha-when-changing-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!