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?
Swing components allow you to change the background color of a component simply by using the setBackground() method. It is also possible to use a Color created with an “alpha” value. The alpha value defines the transparency of a Color.
The alpha value defines the transparency of a color and can be represented by a float value in the range 0.0 - 1.0 or 0 - 255. An alpha value of 1.0 or 255 means that the color is completely opaque and an alpha value of 0 or 0.0 means that the color is completely transparent.
You can actually apply a hex code color that is transparent. The hex code for transparent white (not that the color matters when it is fully transparent) is two zeros followed by white's hex code of FFFFFF or 00FFFFFF.
The transparency object represents image data that is guaranteed to be either completely opaque (alpha value of 1.0) or completely transparent (alpha value of 0.0). static int. OPAQUE. The transparency object represents image data that is guaranteed to be completely opaque (all pixels have an alpha value of 1.0).
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 );
You can call the constructor of Color in the following way:
Color c = new Color(r,g,b,a);
where a is the alpha (transparency) value.
As with all Java classes, you can find this information in the official API: http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html
It's a really good resource and can spare you waiting for an answer on here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With