TechAlpine – The Technology world

How to create Translucent and Shaped Windows in Java?

Overview: In this article we will discuss about the transparency and different shaped windows in Java. In java 7 swing supports this feature and making swing UI components more flexible and user friendly.

Introduction: In some application, transparent window is a requirement to support its functionality. Now in Java, transparency can be implemented. A translucent window is created by altering its opacity by implementing a method called setOpacity on a JFrame component. But we must understand that a translucent window is only possible if the underlying operating system supports it. And we also need to make sure that the window is not decorated. To make a window undecorated, you need to call setUndecorated (true) method. Some time it is also required to change the shape of a window UI. To implement it, we need to call setShape method within the componentResized method. It will recalculate the shape when the window is resized.

Back ground: In java UI, support for translucency and shaped window was a long time demand for Swing/AWT components. For native development access to these properties are available from long time back. But it was not accessible to core java components. In Java6 onward, support for translucency and shaped window is available. Even per pixel level translucency is also supports in Java7.

Type of support for translucency and transparency: Java 7 supports the following three type of support.

TRANSLUCENT: In this feature the window can have different colors but with the same level of opacity. So the alpha value is same for all the pixels. This effect is suitable for fade off a window and then gradually decreases and increases the alpha value.

PERPIXEL_TRANSLUCENT: This property supports different alpha values within the window itself. This is used to fade selected region of the window.

PERPIXEL_TRANSPARENT: This property supports different shapes of windows rather than traditional rectangle. If per pixel transparency is supported then different shapes like circle, triangle can be created.

Before we use any of the above properties we need to test the support of under lying operating system. The test can be done by using method isWindowTranslucencySupported belonging to the java.awt.GraphicsDevice class. It takes the type of transparency as input and returns true/false to indicate the support.

Let us check a sample code to implement the translucency feature. In this example we have created a rectangular translucent window on a text area. First we have disabled the decoration mode to get the effect of transparency. Then we have checked if the underlying operating system supports it or not. If supported the effect is visible on the frame window.

Listing1: Sample showing Translucency of a JFrame window

import java.awt.Color;

import java.awt.GraphicsDevice;

import java.awt.GraphicsEnvironment;

import java.awt.GridBagLayout;

import java.awt.event.ComponentAdapter;

import java.awt.event.ComponentEvent;

import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;

import javax.swing.JTextArea;

import javax.swing.SwingUtilities;

public class TransRecFrame extends JFrame {


* Create a transparent rectangular frame with 85% transparency


public TransRecFrame() {

super(“Translucent Rectangular Frame”);

//Set layout

setLayout(new GridBagLayout());

//Create a text area

final JTextArea txtArea = new JTextArea(5, 50);



//Call to disable decoration



//Call setShape to resize the shape when widnow is resized

addComponentListener(new ComponentAdapter() {


public void componentResized(ComponentEvent e) {

setShape(new Rectangle2D.Double(0, 0, getWidth(), getHeight()));



//Make the window 85% transparent


//Set other parameters


setSize(200, 300);




public static void main(String[] args) {

//Create graphics environment

GraphicsEnvironment genv = GraphicsEnvironment


//Check if OS supports Translucency

if (genv.getDefaultScreenDevice().isWindowTranslucencySupported(

GraphicsDevice.WindowTranslucency.TRANSLUCENT)) {

System.out.println(“OS supports translucency”);

new TransRecFrame();




Features to support translucency and shape: In Java7 there are mainly three features available to support translucency and shape of a window.

  • Full window translucency: In this approach full window is translucent.
  • Per pixel translucency: In this approach a part of the window is translucent
  • Shaped windows: Make different shaped windows like ovale, circle, rectangular etc.

Per pixel translucency:

We have already seen how to make a complete window translucent in the previous example. Now we will discuss the second part of making a set of pixels translucent by using their background color. There are some limitations to implement this scenario. The window should not be full screen and the system must support pixel level translucency. The rest of the procedure is similar to the above example.

In the following example we will see how pixel level translucency is set in a frame.

Listing2: Sample showing pixel level translucency support.

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class PixelTranslucency extends JFrame {

public PixelTranslucency() {

super(“Set pixel level translucency”);

//Set size of the JFrame

setSize(250, 250);

//Set lay out

getContentPane().setLayout(new GridLayout(6, 6));

//Call for pixel setting

for(int i = 0; i < 16; i++){

add(new PixelPanel(255 – i * 12));


//Set background and other properties

setBackground(new Color(0, 0, 0, 0));




public static void main(String[] args) {

GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();

//Check if window supports translucency

if (genv.getDefaultScreenDevice().isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT)) {

System.out.println(“OS supports translucency”);

new PixelTranslucency();


}     private class PixelPanel extends JPanel {

private PixelPanel(int pixl) {


setBackground(new Color(0, 0, 255, pixl));




Shaped Windows:

Now we will discuss about another important feature supported by Java7.The shaped window supports all types of shapes whatever be the requirement of the user. This feature helps you to create any shape like circle, triangle, polygon or any possible complex shape. The setShape method of the window class is available for setting the property. But again we have to remember that full screen mode should not be allowed and the operating system supports translucency.

The following example shows the usage of shaped window.

Listing3: The sample code showing the usage of shaped window.

import javax.swing.*;

import java.awt.*;

import java.awt.geom.Ellipse2D;

import java.awt.geom.GeneralPath;

public class JavaShapedWindow extends JFrame {

public JavaShapedWindow() {

super(“Set shaped Window”);

//Set undecorated OFF to get an effect


//Set size

setSize(new Dimension(250, 250));

//Set polygon properties

Polygon polygon = new Polygon();

polygon.addPoint(0, 100);

polygon.addPoint(50, 0);

polygon.addPoint(100, 100);

//Set the values of the shape

Ellipse2D.Double newCircle = new Ellipse2D.Double(0, 50, 1.0*100, 1.0*100);

//Set general path properties

GeneralPath gpath = new GeneralPath();

gpath.append(polygon, true);

gpath.append(newCircle, true);

//Set the path


//Set box layout

getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));


//Create label and set properties

JLabel newlabel = new JLabel(“Java Shaped window”);




//Set content pane background color


//Set location




public static void main(String[] args) {

GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();

//Check OS support for this property

if (genv.getDefaultScreenDevice().isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)) {

System.out.println(“OS supports translucency”);

new JavaShapedWindow();




We can also implement a combination of two features like translucency and shaped window. To implement this, call setOpacity method to your frame. The result window will display the combined effect. But we should remember the underlying operating system must support pixel level translucency and pixel level transparency.


In this article we have covered some new features in java7.The translucency and shaped window features are really interesting and make java UI components more flexible. But at the same time we must remember that the underlying operating system should support all these properties. Otherwise the effect will not be visible. So in the application itself we check the OS support and then implement the effect. The latest feature of java allows the developers to create different types of effect on the UI side. As we all know that look and feel is an important aspect of any UI based application. So the developer is now equipped with tools to make the UI more pleasant and attractive.

Leave a Reply

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

8 − 8 =

TechAlpine Books