bitQUAKE
bitQUAKE

Reputation: 503

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

Upvotes: 1

Views: 251

Answers (1)

Josh Marinacci
Josh Marinacci

Reputation: 1745

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

Ex:

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

Upvotes: 1

Related Questions