The following example creates a window that is 55 percent translucent and 45 percent opaque.
Code:
import java.awt.*;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
public class TranslucentWindow extends JFrame {
public TranslucentWindow() {
super("TranslucentWindow");
setLayout(new GridBagLayout());
setSize(330,180);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JButton("This is Button"));
}
public static void main(String[] args) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
System.err.println(
"Translucency is not supported");
System.exit(0);
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TranslucentWindow tw = new TranslucentWindow();
tw.setOpacity(0.45f);
tw.setVisible(true);
}
});
}
}
Bookmarks