javaswingrenderingtransparencyglasspane

Java GlassPanel clear transparent background


I want to render something on my GlassPane. The problem is, that if i move the rendered lines around, the previously rendered pixels have still the same color. I can not use g.clearRect because it doesn`t clears the transparency.

Thats my rendering code:

Graphics2D g2 = (Graphics2D) g;

    for(LinePath line : lines)
    {
        for(int i = 0; i < line.points.length; i+=2)
        {
            if(i != 0)
            {
                g.drawLine((int)line.points[i-2],(int)line.points[i-1],(int)line.points[i],(int)line.points[i+1]);
            }
        }
    }


    //Clearing alpha
    Area area = new Area();
    // This is the area that will filled...
    area.add(new Area(new Rectangle2D.Float(0, 0, getWidth(), getHeight())));
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.0f));
    g2.fill(area);

And here is the result: enter image description here


Solution

  • clearRect should work but you have to reset your alpha composite before using it.

    Ex:

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1.0f));