 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create gradient translucent windows in Java Swing
With JDK 7, we can create a gradient based translucent window using swing very easily. Following are the steps needed to make a gradient-based translucent window.
Make the background of JFrame transparent first.
frame.setBackground(new Color(0,0,0,0));
Create a gradient paint, and fill the panel.
JPanel panel = new javax.swing.JPanel() {    protected void paintComponent(Graphics g) {       Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),        getWidth(), getHeight(), new Color(R, G, B, 255), true);       Graphics2D g2d = (Graphics2D)g;       g2d.setPaint(p);       g2d.fillRect(0, 0, getWidth(), getHeight());    } } Assign the panel as a content pane to the frame.
frame.setContentPane(panel);
Example
See the example below of a window with gradient-based translucency.
import java.awt.Color; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagLayout; import java.awt.Paint; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.UnsupportedLookAndFeelException; public class Tester {    public static void main(String[] args)               throws ClassNotFoundException, InstantiationException,               IllegalAccessException, UnsupportedLookAndFeelException {               JFrame.setDefaultLookAndFeelDecorated(true);       // Create the GUI on the event-dispatching thread       SwingUtilities.invokeLater(new Runnable() {          @Override          public void run() {             createWindow();                                }       });    }    private static void createWindow() {                 JFrame frame = new JFrame("Translucent Window");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       createUI(frame);       frame.setVisible(true);              }    private static void createUI(JFrame frame){       frame.setLayout(new GridBagLayout());       frame.setSize(200, 200);                   frame.setLocationRelativeTo(null);       frame.setBackground(new Color(0,0,0,0));       JPanel panel = new javax.swing.JPanel() {          protected void paintComponent(Graphics g) {             if (g instanceof Graphics2D) {                final int R = 100;                final int G = 100;                final int B = 100;                                Paint p =                   new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),                   getWidth(), getHeight(), new Color(R, G, B, 255), true);                Graphics2D g2d = (Graphics2D)g;                g2d.setPaint(p);                g2d.fillRect(0, 0, getWidth(), getHeight());             } else {                super.paintComponent(g);             }          }       };       panel.setLayout(new GridBagLayout());       panel.add(new JButton("Hello World"));       frame.setContentPane(panel);    } } Output

Advertisements
 