How to Create Translucent and Shaped Windows in java.

Hi all,

I am back after a long time. This time I want to tell you all how I set a window as translucent in java. It was a very cumbersome thing to do before recently. From JDK 6 update 10, java has this facility to set the window as translucent  and its very easy.

Recently I did this in one of my projects and wanted to share it with you all.

As of the Java Platform, Standard Edition 6 (Java SE 6) Update 10 release, we can add translucent and shaped windows to our Swing applications.

The translucent and shaped window API was first added to the Java SE 6 Update 10 release as a private API. This functionality was moved to the public AWT package in the JDK 7 release.

Here is an example :

import java.awt.*;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;

public class MyWindow extends JFrame {
    public MyWindow() {
        super("MyWindow");
        setLayout(new GridBagLayout());

        setSize(300,200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        add(new JButton("Just a Button"));
    }

    public static void main(String[] args) {
        // Determine if the GraphicsDevice supports translucency.
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 GraphicsDevice gd = ge.getDefaultScreenDevice();

        //If translucent windows aren't supported, exit.
        if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) { 
 System.err.println( "Translucency is not supported"); 
 System.exit(0); 
 }

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                MyWindow tw = new MyWindow();

		// Set the window to 55% opaque (45% translucent).
                tw.setOpacity(0.55f);

		// Display the window.
		tw.setVisible(true);
	    }
        });
    }
}

To learn more please check this link.

 

Here is how you can select which methods to use according to your JDK version.

 


Regards,
Manohar Bhattarai (मनोहर भट्टराई)
http://about.me/manoharbhattarai
http://www.facebook.com/pages/Manohar-Bhattarai/130973796914160
Blogs:
http://manoharbhattarai.wordpress.com/
http://manoharbhattarai.posterous.com/
http://manoharbhattarai.blogspot.com/
Microblogs:
Twitter :- http://twitter.com/mhrbhattarai
Identi.ca :- http://identi.ca/manoharbhattarai

About these ads

Tags: , , , , , , , , , , , , , , , , , , , , , ,

4 Responses to “How to Create Translucent and Shaped Windows in java.”

  1. geox Says:

    That is the proper blog for anybody who needs to find out about this topic. You realize so much its virtually exhausting to argue with you (not that I really would need…HaHa). You undoubtedly put a brand new spin on a subject thats been written about for years. Great stuff, just nice!

  2. hermes Says:

    I love blogs and i also can tell you additionally like blogs.

  3. Free Says:

    […]in this page you can find our links to articles , we noticed these are worth visiting[…]…

  4. code sharing Says:

    add your code…

    […]How to Create Translucent and Shaped Windows in java. « Manohar Bhattarai[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 350 other followers

%d bloggers like this: