//Titel:            Dialog-Fenster
//Version:
//Copyright:		Copyright (c) 2010
//Autor:
//Organisation:
//Beschreibung:


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


public class GlassPaneDemo2 extends JFrame {
  private MyGlassPane myGlassPane;

  JButton BnOk = new JButton("Ok");
  JButton BnEsc = new JButton("Abbruch");

  public GlassPaneDemo2() {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  private void setGUI() {
    setSize(400, 230);
    setTitle("JFrame als Dialog");

    JCheckBox changeButton = new JCheckBox("Glass pane \"visible\"");
    changeButton.setSelected(false);
    changeButton.setFont( new Font("Arial", Font.BOLD,18)  );

    BnOk.setFont( new Font("Arial", Font.BOLD,18)  );
    BnEsc.setFont( new Font("Arial", Font.BOLD,18)  );
    BnOk.setPreferredSize( BnEsc.getPreferredSize() );

    this.getContentPane().setLayout( new BorderLayout() );
    this.getContentPane().add( changeButton, BorderLayout.NORTH );


	JPanel pnButton = new JPanel();
	//pnButton.setBackground(Color.red);
	pnButton.setLayout( new FlowLayout() );
	pnButton.add(BnOk);
	pnButton.add(BnEsc);

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

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


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


     this.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        this_mouseClicked(e);
         System.out.println("     mouseClicked");
      }

      public void mouseEntered(MouseEvent e) {
        this_mouseEntered(e);
      }

      public void mouseExited(MouseEvent e) {
        this_mouseExited(e);
      }

      public void mousePressed(MouseEvent e) {
        this_mousePressed(e);
      }

      public void mouseReleased(MouseEvent e) {
        this_mouseReleased(e);
      }
    });

      myGlassPane = new MyGlassPane( this.getContentPane() );
      this.setGlassPane(myGlassPane);
      myGlassPane.updateUI();
    

  } // setGUI

  
   void this_mouseClicked(MouseEvent e) {
      System.out.println("mouseClicked");
     Point point = e.getPoint();
     Point p = SwingUtilities.convertPoint( this.getContentPane(), point, myGlassPane );
     System.out.println("pointX: "+point.x+"  pointY: "+point.y+"      px: "+p.x+"  p.y: "+p.y);

     myGlassPane.setPoint( e.getPoint() );
     // this.paint( this.getGraphics() );
    }
  
  
    void this_mouseEntered(MouseEvent e) {
      System.out.println("mouseEntered");
    }
  
    void this_mouseExited(MouseEvent e) {
      System.out.println("mouseExited");
    }
  
    void this_mousePressed(MouseEvent e) {
      System.out.println("mousePressed");
    }
  
    void this_mouseReleased(MouseEvent e) {
      System.out.println("mouseReleased");
    }
  

    private void BnOk_Click() {
        System.out.println("Hier in Ok");
    }

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

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

}  // GlassPaneDemo2



class MyGlassPane extends JComponent { // implements ItemListener {
    private Point point;
    private boolean bActive;
    private Container contentPane;

    public MyGlassPane(Container contentPane) {
       this.contentPane = contentPane;
        bActive=false;
        point=null;
        setOpaque(true);
        setGUI();
// public void javax.swing.JComponent.setOpaque(boolean)

    }

   private void setGUI() {
      System.out.println("setGUI");
    
   }


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

    System.out.println("in paint");
        // Löschen des Bildschirms
        g.setColor(Color.BLUE);
        //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 setPoint(Point p) {
    point = SwingUtilities.convertPoint( this, p, contentPane);
     System.out.println("px: "+p.x+"  p.y: "+p.y+"  ppx: "+point.x+"  pp.y: "+point.y);
        point.y = p.y-23;
        int w = getWidth();
        int h = getHeight();
       // this.repaint( new Rectangle(0,0,w,h) );

        this.paint( getGraphics() );
        //repaint();
/*
        Graphics g = getGraphics();
        int w = getWidth();
        int h = getHeight();
        g.setColor(Color.BLUE);
        //g.fillRect(0,0, w,h );

    System.out.println("set Point: h:"+h+"   w:"+w);
*/
    }

}