[ Back | Previous | Next ]

Watermark/transparency in swing?

Package:
javax.swing.*
Product:
Swing
Release:
1.1.x
Related Links:
ComboBox
JDialog
JFileChooser
JFrame
JOptionPane
JProgressBar
JScrollPane
JTable
General
JTree
JWindow
KeyStroke
LayeredPane
UIDefaults
Comment:
Before showing the code, here's that "Jazz It Up" 5-step plan:

 1. Setting up: You'll need at least one GIF or JPG image for the background. You'll want one that
 is designed to be tiled. 

 2. Opacity: The first thing you'll need to do is call setOpaque(false) on your JFrame's content
 pane, labels, and on all your panels. You also want to make sure your label foreground color will
 be readable over your background. For example,. if you have a dark background image, use
 white for your label foreground.

 3. Backgrounds: When you call setOpaque(false), the affected component will no longer fill its
 background, so you can remove, or comment out, any setBackground() code for these items.

 4. Transparency: First of all, you can't use Colors with alpha values unless you are using JavaŽ
 2. That said, if you want to make a component partially transparent (like the text fields shown
 above), first call setOpaque(true) on it. Then assign background Colors that have been
 constructed with alpha values, in this fashion:

     new Color(int red, int green, int blue, int alpha)
     new Color(float red, float green, float blue, float alpha)

 5. Putting it all together: Finally, you'll need to add the background to your JLayeredPane. You'll
 also want it to tile itself dynamically, so that no matter how big you make your frame, the
 background will be filled with the image. First load your background image as an ImageIcon. You
 can specify a filename to be loaded from your codebase or current directory, or a URL:





import javax.swing.*;
 import java.awt.*;

 public class FancyFrame extends JFrame
 {
     public FancyFrame()
     {
         super("Fancy Shmancy");
         setSize(256,256);
         
         JPanel content = new JPanel();
         content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
         content.setOpaque(false);
         
         JLabel label1 = new JLabel( "Username:" );
         label1.setForeground(Color.white);
         content.add( label1 );
         
         JTextField field = new JTextField(15);
         field.setBackground(new Color(255,255,255,80));
         content.add( field );
         
         JLabel label2 = new JLabel( "Password:" );
         label2.setForeground(Color.white);
         content.add( label2 );
         
         JPasswordField fieldPass = new JPasswordField(15);
         fieldPass.setBackground(new Color(255,255,255,80));
         content.add( fieldPass );
         
         getContentPane().setLayout(new FlowLayout());
         getContentPane().add(content);
         ((JPanel)getContentPane()).setOpaque(false);
         
         final ImageIcon m_image = new ImageIcon("mmaze.gif");
         final int winc = m_image.getIconWidth();
         final int hinc = m_image.getIconHeight();
         JLabel backlabel = new JLabel("");
         if (m_image.getIconWidth() > 0 && m_image.getIconHeight() > 0)
         {
             backlabel = new JLabel() {
                 public void paintComponent(Graphics g) {
                     int w = getParent().getWidth();
                     int h = getParent().getHeight();
                     for (int i=0;i<h+hinc;i=i+hinc)
                     {
                         for (int j=0;j<w+winc;j=j+winc)
                         {
                             m_image.paintIcon(this,g,j,i);
                         }
                     }
                 }
                 public Dimension getPreferredSize() {
                     return new Dimension(super.getSize());
                 }
                 public Dimension getMinimumSize() {
                     return getPreferredSize();
                 }
             };
         }
         
         getLayeredPane().add(backlabel, new Integer(Integer.MIN_VALUE));
         backlabel.setBounds(0,0,5000,5000);
         
         setVisible(true);
     }
     
     public static void main(String[] args)
     {
         new FancyFrame();
     }
 }