- Simple Translucency
- Per-Pixel Translucency
- Per-Pixel Transparency and Shaped Windows
- Combining Translucency with Per-Pixel Transparency and Shaped Windows
- Conclusion
Combining Translucency with Per-Pixel Transparency and Shaped Windows
You can combine per-pixel (or even simple) translucency with per-pixel transparency to achieve a translucent shaped window. For proof, check out Listing 4, whose CTSWDemo application source code extends the previous application to also include per-pixel translucency.
Listing 4CTSWDemo.java
// CTSWDemo.java import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Shape; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Ellipse2D; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class CTSWDemo extends JFrame { public CTSWDemo () { super ("Combined Translucency with Per-Pixel Transparency and Shaped "+ "Window Demo"); setUndecorated (true); // Avoid decorated window artifacts. JPanel gradPanel = new JPanel () { Color colorA = new Color (255, 0, 0, 0); Color colorB = new Color (255, 0, 0, 255); protected void paintComponent (Graphics g) { Graphics2D g2d = (Graphics2D) g; GradientPaint gp; gp = new GradientPaint (0.0f, 0.0f, colorA, 0.0f, getHeight (), colorB, true); g2d.setPaint (gp); g2d.fillRect (0, 0, getWidth (), getHeight ()); } }; gradPanel.setPreferredSize (new Dimension (300, 200)); gradPanel.setLayout (new BoxLayout (gradPanel, BoxLayout.Y_AXIS)); JButton btnClose = new JButton ("Close"); ActionListener al; al = new ActionListener () { public void actionPerformed (ActionEvent ae) { System.exit (0); } }; btnClose.addActionListener (al); btnClose.setAlignmentX (0.5f); gradPanel.add (Box.createVerticalGlue ()); gradPanel.add (btnClose); gradPanel.add (Box.createVerticalGlue ()); setContentPane (gradPanel); if (!getGraphicsConfiguration ().isTranslucencyCapable ()) { System.err.println ("per-pixel translucency not in effect for this "+ "graphics configuration"); System.exit (0); } setBackground (new Color (0, 0, 0, 0)); // Achieve per-pixel // translucency. pack (); setShape (new Ellipse2D.Float (0, 0, getWidth (), getHeight ())); setLocationRelativeTo (null); setVisible (true); } public static void main (String [] args) { Runnable r; r = new Runnable () { public void run () { GraphicsEnvironment ge; ge = GraphicsEnvironment.getLocalGraphicsEnvironment (); if (!ge.getDefaultScreenDevice (). isWindowTranslucencySupported (GraphicsDevice.WindowTranslucency. PERPIXEL_TRANSLUCENT)) { System.err.println ("per-pixel translucency isn't "+ "supported"); return; } if (!ge.getDefaultScreenDevice (). isWindowTranslucencySupported (GraphicsDevice.WindowTranslucency. PERPIXEL_TRANSPARENT)) { System.err.println ("per-pixel transparency isn't "+ "supported"); return; } new CTSWDemo (); } }; EventQueue.invokeLater (r); } }
Listing 4's panel gradient code once again employs alpha values when creating the pair of Color objects. As Figure 4 shows, however, the translucent red gradient now occupies an elliptical shape portion of the undecorated window instead of the entire window.
Figure 4 The setBackground() method call allows the ellipse-shaped window's interior to be rendered with the gradient of red translucency.