Transparency is the quality of being able to see through a material. The easiest way to understand transparency is to imagine a piece of glass or water. Technically, the rays of light can go through the glass and this way we can see objects behind the glass.

In computer graphics, we can achieve transparency effects using alpha compositing. Alpha compositing is the process of combining an image with a background to create the appearance of partial transparency. The composition process uses an alpha channel. Alpha channel is an 8-bit layer in a graphics file format that is used for expressing translucency (transparency). The extra eight bits per pixel serves as a mask and represents 256 levels of translucency.

The AlphaComposite class is used to work with transparency in Java 2D. It implements the basic alpha compositing rules for combining source and destination pixels to achieve blending and transparency effects with graphics and images. To create an AlphaComposite, you provide two values. The rule designator and the alpha value. The rule specifies how we combine source and destination pixels. Most often it is AlphaComposite.SRC_OVER. The alpha value can range from 0.0f (completely transparent) to 1.0f (completely opaque).

Picture below is game menu I ever made, if you see carefully, there’s rounded transparent rectangle, it make the menu looks good isn’t?  You could also use this style for make some future dashboard effect and many more.

Code below will show you how to make ten rectangles with different transparency level :

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TransparentRectangles extends JPanel {

    public void paint(Graphics g) {

        Graphics2D g2d = (Graphics2D)g;

        for (int i = 1; i <= 10; i++) {
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,i * 0.1f));
            g2d.fillRect(50 * i, 20, 40, 40);

    public static void main(String[] args) {

        JFrame frame = new JFrame("Transparent Rectangles");
        frame.add(new TransparentRectangles());
        frame.setSize(590, 120);

Hope the post useful, thanks for reading.