import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import javax.swing.event.MouseInputAdapter;

import java.awt.event.*;


/** An application that requires no other files. */
public class GlassPaneDemo1  extends JFrame {

    private MyGlassPane myGlassPane;
  JButton BnOk = new JButton("Ok");
  JButton BnEsc = new JButton("Abbruch");
    JCheckBox changeMove;


    public GlassPaneDemo1() {
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(200,300);
      setTitle("GlassPaneDemo");
      setGUI();
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private void setGUI() {
        this.getContentPane().setLayout( new BorderLayout() );

        //Start creating and adding components.

        JPanel pnTop = new JPanel();
        pnTop.setLayout( new FlowLayout() );

        this.getContentPane().add( pnTop, BorderLayout.CENTER );
        JCheckBox changeButton = new JCheckBox("Glass pane \"visible\"");
        changeButton.setSelected(false);
        changeMove = new JCheckBox("Glass pane \"move\"");
        pnTop.add(changeButton);
        pnTop.add(changeMove);

        changeMove.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            changeMove_Click();
          }
        });


        JPanel pnButton = new JPanel();
        pnButton.setLayout( new FlowLayout() );
        pnButton.add(BnOk);
        pnButton.add(BnEsc);

        BnEsc.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            BnEsc_Click();
          }
        });

        this.getContentPane().add( pnButton, BorderLayout.SOUTH );


        //Set up the menu bar, which appears above the content pane.
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        menu.add(new JMenuItem("Do nothing"));
        menuBar.add(menu);
        this.setJMenuBar(menuBar);

        //Set up the glass pane, which appears over both menu bar
        //and content pane and is an item listener on the change
        //button.
        myGlassPane = new MyGlassPane(changeButton, menuBar,
                                      this.getContentPane());
        changeButton.addItemListener(myGlassPane);
        this.setGlassPane(myGlassPane);
        myGlassPane.updateUI();

        //Show the window.
        // pack();
    }

    private void changeMove_Click() {
       myGlassPane.setMove(  changeMove.isSelected() );
    }

    private void BnEsc_Click() {
        setVisible(false);
        System.exit(0);
    }


    public static void main(String[] args) {
        GlassPaneDemo1 frame = new GlassPaneDemo1();
        frame.setVisible(true);
    }
}

/**
 * We have to provide our own glass pane so that it can paint.
 */
class MyGlassPane extends JComponent implements ItemListener {
    Point point;
    CBListener listener;

    //React to change button clicks.
    public void itemStateChanged(ItemEvent e) {
        setVisible(e.getStateChange() == ItemEvent.SELECTED);
    }

    protected void paintComponent(Graphics g) {
        int w = getWidth();
        int h = getHeight();

        // Löschen des Bildschirms
        g.setColor(Color.WHITE);
        // setOpaque(true);
        //g.fillRect(0,0, w,h );

        if (point != null) {
            g.setColor(Color.red);
            g.fillOval(point.x - 10, point.y - 10, 20, 20);
        }
    }

    public void setMove(  boolean bMoveSelected ) {
      listener.setMove(bMoveSelected);
    }

    public void setPoint(Point p) {
        point = p;
    }

    public MyGlassPane(AbstractButton aButton,
                       JMenuBar menuBar,
                       Container contentPane) {
        listener = new CBListener(aButton, menuBar,
                                             this, contentPane);
        addMouseListener(listener);
        addMouseMotionListener(listener);
    }
}

/**
 * Listen for all events that our check box is likely to be
 * interested in.  Redispatch them to the check box.
 */
class CBListener extends MouseInputAdapter {
    Toolkit toolkit;
    Component liveButton;
    JMenuBar menuBar;
    MyGlassPane glassPane;
    Container contentPane;
    boolean bMoveSelected=false;


    public CBListener(Component liveButton, JMenuBar menuBar,
                      MyGlassPane glassPane, Container contentPane) {
        toolkit = Toolkit.getDefaultToolkit();
        this.liveButton = liveButton;
        this.menuBar = menuBar;
        this.glassPane = glassPane;
        this.contentPane = contentPane;
    }

    public void setMove(  boolean bMoveSelected ) {
      this.bMoveSelected = bMoveSelected;
    }

    public void mouseMoved(MouseEvent e) {
        redispatchMouseEvent(e, bMoveSelected);
    }

    public void mouseDragged(MouseEvent e) {
        redispatchMouseEvent(e, false);
    }

    public void mouseClicked(MouseEvent e) {
        redispatchMouseEvent(e, false);
    }

    public void mouseEntered(MouseEvent e) {
        redispatchMouseEvent(e, false);
    }

    public void mouseExited(MouseEvent e) {
        redispatchMouseEvent(e, false);
    }

    public void mousePressed(MouseEvent e) {
        redispatchMouseEvent(e, false);
    }

    public void mouseReleased(MouseEvent e) {
        redispatchMouseEvent(e, true);
    }

    //A basic implementation of redispatching events.
    private void redispatchMouseEvent(MouseEvent e,
                                      boolean repaint) {
        Point glassPanePoint = e.getPoint();
        Container container = contentPane;
        // (Component source, int x, int y, Component destination) 
        Point containerPoint = SwingUtilities.convertPoint(
                                        glassPane,
                                        glassPanePoint,
                                        contentPane);
     System.out.println("px: "+glassPanePoint.x+"  p.y: "+glassPanePoint.y+"  ppx: "+containerPoint.x+"  pp.y: "+containerPoint.y);
        if (containerPoint.y < 0) { //we're not in the content pane
            if (containerPoint.y + menuBar.getHeight() >= 0) { 
                //The mouse event is over the menu bar.
                //Could handle specially.
            } else { 
                //The mouse event is over non-system window 
                //decorations, such as the ones provided by
                //the Java look and feel.
                //Could handle specially.
            }
        } else {
            //The mouse event is probably over the content pane.
            //Find out exactly which component it's over.  
            Component component = 
                SwingUtilities.getDeepestComponentAt(
                                        container,
                                        containerPoint.x,
                                        containerPoint.y);
                            
            if ((component != null) 
                && (component.equals(liveButton))) {
                //Forward events over the check box.
                Point componentPoint = SwingUtilities.convertPoint(
                                            glassPane,
                                            glassPanePoint,
                                            component);
                component.dispatchEvent(new MouseEvent(component,
                                                     e.getID(),
                                                     e.getWhen(),
                                                     e.getModifiers(),
                                                     componentPoint.x,
                                                     componentPoint.y,
                                                     e.getClickCount(),
                                                     e.isPopupTrigger()));
            }
        }
        
        //Update the glass pane if requested.
        if (repaint) {
            glassPane.setPoint(glassPanePoint);
            glassPane.repaint();
        }
    }
}
