GlassPane

GlassPane_Overlay Example (Run Applet)

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

public class GlassPane_Overlay extends JApplet
{
    @Override
    public void init()
    {
        GlassPane glassPane = new GlassPane();
        this.setContentPane(new View(glassPane));
        this.setGlassPane(glassPane);
    }

    public class View extends JPanel implements ItemListener
    {
        private JPanel glassPane;
        private final JRadioButton on = new JRadioButton("On", false);
        private final JRadioButton off = new JRadioButton("Off", true);

        public View(JPanel glassPane)
        {
            super();

            this.glassPane = glassPane;

            // set up a menuBar and a popupMenu
            // to show that menuBars display behind the glasspane
            // and popupMenus display in front of the glasspane
            JMenuBar menuBar = new JMenuBar();
            setJMenuBar(menuBar);
            menuBar.add(new JMenu("Menu bar is behind GlassPane"));

            JPopupMenu popupMenu = new JPopupMenu();
            popupMenu.add(new JMenu("Popup menu is in front of GlassPane"));
            popupMenu.show(this.getRootPane(), 100, 200);

            // Set the glass pane
            // setGlassPane(this.glassPane);
            glassPane.setOpaque(false);
            glassPane.setVisible(true);

            //Group the radio buttons.
            final ButtonGroup group = new ButtonGroup();
            group.add(this.on);
            group.add(this.off);

            // place the radio buttons onto the Applet's panel
            this.add(this.on);
            this.add(this.off);


            this.on.addItemListener(this);
            this.off.addItemListener(this);
        }

        @Override
        public void itemStateChanged(ItemEvent e)
        {
            final Object source = e.getSource();
            if (source == this.on)
            {
                this.glassPane.setVisible(true);
            }
            else if (source == this.off)
            {
                this.glassPane.setVisible(false);
            }
        }
    }

    private final class GlassPane extends JPanel
    {
        @Override
        public void paintComponent(Graphics g)
        {
            g.setColor(Color.blue);
            g.drawOval(0, 0, getWidth() - 1, getHeight() - 1);

            // draw over the two radio buttons
            g.setColor(Color.red);
            g.drawOval(15, 15, getWidth() - 31, getHeight() - 31);
        }
    }
}

GlassPane_Translucent_Demo Example (Run Applet)

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

public class GlassPane_Translucent_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ItemListener
    {
        private final TranslucentGlassPane glassPane = new TranslucentGlassPane();
        private final JRadioButton on = new JRadioButton("On", true);
        private final JRadioButton off = new JRadioButton("Off", false);

        public View()
        {
            super();

            final JMenuBar menuBar = new JMenuBar();
            final JLabel koalaBear = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("images/koala.jpg")));

            // Set the glass pane
            setGlassPane(this.glassPane);
            this.glassPane.setOpaque(false);
            this.glassPane.setVisible(true);

            //Group the radio buttons.
            final ButtonGroup group = new ButtonGroup();
            group.add(this.on);
            group.add(this.off);

            // place the radio buttons onto the Applet's panel
            this.add(this.on);
            this.add(this.off);
            this.add(koalaBear);

            this.on.addItemListener(this);
            this.off.addItemListener(this);
        }

        @Override
        public void itemStateChanged(ItemEvent e)
        {
            final Object source = e.getSource();
            if (source == this.on)
            {
                this.glassPane.setVisible(true);
            }
            else if (source == this.off)
            {
                this.glassPane.setVisible(false);
            }
        }
    }

    public class TranslucentGlassPane extends JPanel
    {
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            final BufferedImage bImg = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
            final Graphics2D g2d = bImg.createGraphics();

            for (int x = 0; x < getWidth(); x++)
            {
                for (int y = 0; y < getHeight(); y++)
                {
                    bImg.setRGB(x, y, new Color(255, 255, 255, 200).getRGB());  // alpha set less than 255
                }
            }

            g.drawImage(bImg, 0, 0, getWidth(), getHeight(), this);
        }
    }
}

GlassPane_BlockMouse Example (Run Applet)

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

public class GlassPane_BlockMouse_Demo extends JApplet
{
    @Override
    public void init()
    {
        final GlassPane glassPane = new GlassPane();
        this.setContentPane(new View(glassPane));
        this.setGlassPane(glassPane);
    }

    public class View extends JPanel implements ItemListener
    {
        private JPanel glassPane;
        private final JRadioButton on = new JRadioButton("On", false);
        private final JRadioButton off = new JRadioButton("Off", true);

        public View(JPanel glassPane)
        {
            super();

            this.glassPane = glassPane;

            // set up a menuBar and a popupMenu
            // to show that menuBars display behind the glasspane
            // and popupMenus display in front of the glasspane
            final JMenuBar menuBar = new JMenuBar();
            setJMenuBar(menuBar);
            menuBar.add(new JMenu("Menu bar is behind GlassPane"));

            final JPopupMenu popupMenu = new JPopupMenu();
            popupMenu.add(new JMenu("Popup menu is in front of GlassPane"));
            popupMenu.show(this.getRootPane(), 100, 200);

            // Set the glass pane
            // setGlassPane(this.glassPane);
            glassPane.setOpaque(false);
            glassPane.setVisible(true);

            //Group the radio buttons.
            final ButtonGroup group = new ButtonGroup();
            group.add(this.on);
            group.add(this.off);

            // place the radio buttons onto the Applet's panel
            this.add(this.on);
            this.add(this.off);


            this.on.addItemListener(this);
            this.off.addItemListener(this);
        }

        @Override
        public void itemStateChanged(ItemEvent e)
        {
            final Object source = e.getSource();
            if (source == this.on)
            {
                this.glassPane.setVisible(true);
            }
            else if (source == this.off)
            {
                this.glassPane.setVisible(false);
            }
        }
    }

    class GlassPane extends JPanel implements MouseListener
    {
        private boolean mouseClicked = false;
        private int x = 0;
        private int y = 0;

        GlassPane()
        {
            super();
            this.addMouseListener(this);
        }

        @Override
        public void paintComponent(Graphics g)
        {
            if (this.mouseClicked)
            {
                g.setColor(Color.red);
                g.fillOval(this.x - 10, this.y - 10, 20, 20);
            }
        }

        @Override
        public void mouseClicked(MouseEvent me)
        {
        }

        @Override
        public void mousePressed(MouseEvent e)
        {
            this.mouseClicked = true;
            this.x = e.getX();
            this.y = e.getY();
            this.repaint();
        }

        @Override
        public void mouseReleased(MouseEvent me)
        {
        }

        @Override
        public void mouseEntered(MouseEvent me)
        {
        }

        @Override
        public void mouseExited(MouseEvent me)
        {
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>