The Java AWT offer various possibilities about window manipulation. Today I’ll talk about two possibilities using class AWTUtilities.

Building a transparent window

To make a Window (JFrame, JDialog …) with alpha effect, you should invoke the method AWTUtilities.setWindowOpacity. This method parameters are: the window to apply transparency and transparency degree, that can be between 0 (zero) and 1, being 0 invisible and 1 totally visible.

JFrame  window = new JFrame("My Window");

//70% of transparency
AWTUtilities.setWindowOpacity(window, .7f);
window.setSize(800,600);
window.setVisible(true);

The result will be:

Changing window shape

To change the window shape you should use the method AWTUtilities.setWindowShape. This method parameters are: window that will be changed and the new shape (java.awt.Shape) of the window.

The most efficiently form to use is to implement the method componentResized(), cause you may recompute the window and components size

Here I’ll use a triangle with 70% of transparency.

final JFrame  window = new JFrame("My Window");

try {
	//Add the ComponentListener to implement componentResized method
	window.addComponentListener(new ComponentAdapter(){
		@Override
		//building componentResized
		public void componentResized(ComponentEvent e) {
			int[] x = {0,400,800}; //Pontos X do polígono
			int[] y = {600,0,600}; //Pontos Y do polígono

			//Triangle with 800 w x 600 h
			Shape shape = new Polygon(x, y, 3);

			AWTUtilities.setWindowShape(window, shape);

			//70% of transparency
			AWTUtilities.setWindowOpacity(window, 0.7f);
		}
	});
} catch (SecurityException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IllegalArgumentException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

window.setUndecorated(true); //removing title bar
window.setSize(800,600);
window.setVisible(true);

Notice that the method setUndecorated( ) was invoked with true as value. This method are responsible by hide the title bar (one with icon and the maximize, minimize and close buttons). This is really necessary, because with title bar visible the window are not be shaped.

The result:

I hope you enjoy… ;)