Translucent Window in Java 7

Let us define three terms – transparent, translucent, and opaque, before we discuss how to use a translucent window in Swing. If something is transparent, you can see through it. For example, clear water is transparent. If something is opaque, you cannot see through it. For example, a concrete wall is opaque. If something is translucent, you can see through it, but not clearly. If something is translucent, it partially allows light to pass through. For example, a plastic curtain is translucent. The terms, “transparent” and “opaque”, describe two opposite states, whereas the term “translucent” describes a state between “transparent” and “opaque”.

Java 7 lets you define the degree of translucency of a window, e.g., a JFrame. A 90% translucent window is 10% opaque. The degree of translucency for a window can be defined using the alpha value of the color component for a pixel. You can define the alpha value of a color using the constructors of the Color class as shown below.

Color(int red, int green, int blue, int alpha)

Color(float red, float green, float blue, float alpha)

The value for the alpha argument is specified between 0 and 255, when the color components are specified in terms of int values. For the float type arguments, its value is between 0.0 and 1.0. The alpha value of 0 or 0.0 means transparent (100% translucent or 0% opaque). The alpha value of 255 or 1.0 means opaque (0% translucent or not transparent).

Java 7 supports three forms of translucency in a window, which are represented by the following three constants in the WindowTranslucency enum.

  • PERPIXEL_TRANSPARENT: In this form of translucency, a pixel in a window is either opaque or transparent. That is, the alpha value for a pixel is either 0.0 or 1.0.
  • TRANSLUCENT: In this form of translucency, all pixels in a window have the same translucency, which can be defined by an alpha value between 0.0 and 1.0.
  • PERPIXEL_TRANSLUCENT: In this form of translucency, each pixel in a window can have its own alpha value between 0.0 and 1.0. This form lets you define the translucency in a window on a per pixel basis.

Click here to read the complete post.


Leave a Reply

Your email address will not be published. Required fields are marked *