Java Graphics2D transparent background

Lajos Arpad picture Lajos Arpad · May 7, 2013 · Viewed 56.9k times · Source

I have a Graphics2D object and I want to set up the background of the object. It has a setBackground method, which has a Color parameter. This way I can set the color of the background.

My question is: how can I set the transparency of the background of the object? Can I somehow tell it to be completely transparent? Can I somehow tell it to be completely opaque? Can I somehow tell it to have 0.8 transparency/opacity? How can I set these values?

I have seen that there are int predefined values called TRANSLUCENT and OPAQUE, but I am not sure how can I use them.

Maybe the correct usage is to call the constructor of Color with an int parameter?

Answer

Extreme Coders picture Extreme Coders · May 7, 2013

You can construct a Color object by specifying a transparency. For example the following code constructs a RED color with 50% transparency

Color c=new Color(1f,0f,0f,.5f );