Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Graphics2D transparent background

Tags:

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?

like image 563
Lajos Arpad Avatar asked May 07 '13 15:05

Lajos Arpad


People also ask

How do you make a transparent background in Java?

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.

Is there a transparent color in Java?

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.

Is there a color code for 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.

What is transparency in Java?

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).


2 Answers

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 ); 
like image 170
Extreme Coders Avatar answered Sep 21 '22 01:09

Extreme Coders


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.

like image 23
RaptorDotCpp Avatar answered Sep 21 '22 01:09

RaptorDotCpp