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

import java.awt.event.*;  // neu

// ohne Klasse Haus
// KOnstruktor Canvas
// gezeichnet wird nur in CCanvas

public class nikolaus2 extends JFrame {

    CCanvas canvas;
    JCheckBox cbHaus = new JCheckBox("Haus",true);
    JCheckBox cbDach = new JCheckBox("Dach",true);
    JCheckBox cbZiegel = new JCheckBox("Ziegel",true);
    JCheckBox cbDiagonale = new JCheckBox("Diagonale",true);
    JCheckBox cbKreise = new JCheckBox("Kreise",true);
    JCheckBox cbSchrift = new JCheckBox("Schrift",true);

   
    
    /** Creates a new instance of grafik3 */
    public nikolaus2() {
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setGUI();
    }
    
    
    private void setGUI() {        
        canvas = new CCanvas();
        this.getContentPane().add(canvas, BorderLayout.CENTER);        
        JPanel panel = new JPanel();
        this.getContentPane().add(panel, BorderLayout.SOUTH);
        panel.setLayout( new GridBagLayout() );
        panel.add(cbHaus,
            new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE,
            new Insets(0,20,0,0), 0, 0));

        panel.add(cbDach,
            new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE,
            new Insets(0,20,0,0), 0, 0));
        

        
        panel.add(cbZiegel,
            new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE,
            new Insets(0,20,0,0), 0, 0));
    
        
        panel.add(cbDiagonale,
            new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE,
            new Insets(0,20,0,0), 0, 0));
    

        panel.add(cbKreise,
            new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE,
            new Insets(0,20,0,0), 0, 0));
    

        panel.add(cbSchrift,
            new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE,
            new Insets(0,20,0,0), 0, 0));

        
           // Events
        cbHaus.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            cbBox_click(e);
          }
        });
        cbDach.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            cbBox_click(e);
          }
        });
        cbZiegel.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            cbBox_click(e);
          }
        });
        cbDiagonale.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            cbBox_click(e);
          }
        });
        cbKreise.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            cbBox_click(e);
          }
        });
        cbSchrift.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            cbBox_click(e);
          }
        });


        
    } // setGUI

    private void cbBox_click(ActionEvent e) {
        if (e.getSource() ==cbHaus )  canvas.setHaus( cbHaus.isSelected() );
        
        if (e.getSource() ==cbDach )  canvas.setDach( cbDach.isSelected() );

        if (e.getSource() ==cbZiegel )  canvas.setZiegel( cbZiegel.isSelected() );
        
        if (e.getSource() ==cbDiagonale )  canvas.setDiagonale( cbDiagonale.isSelected() );

        if (e.getSource() ==cbKreise )  canvas.setKreise( cbKreise.isSelected() );
        
        if (e.getSource() ==cbSchrift )  canvas.setSchrift( cbSchrift.isSelected() );
    
        canvas.update( canvas.getGraphics() );
        //canvas.invalidate();
    }

    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        nikolaus2 f = new nikolaus2();
        f.setSize(600,300);
        f.setVisible(true);
    }
    
}


// Baue eine neue Klasse abgeleitet von Canvas
// hier fehlt natürlich der OBERE Rand  !!!!!!
class CCanvas extends Canvas {

    private boolean _bHaus=true;
    private boolean _bDach=true;
    private boolean _bZiegel=true;
    private boolean _Diagonale=true;
    private boolean _bKreise=true;
    private boolean _bSchrift=true;
    
    public void paint (Graphics g)
    {
        int w = getWidth()-20;
        int h = getHeight()-20;
        int n, top, left;

        // Löschen des Bildschirms
        g.setColor(Color.WHITE);
        g.fillRect(0,0, getWidth(), getHeight() );

        // zentrieren
        // 3 Fälle  Quadrat,  horizontaler  Zylinder, Vertikaler Zylinder
        if (w*1.5 < h) {
            n = w;  // horizontaler  Zylinder
        }
        else {
            n = (int) (h/1.5);  // senkrechter  Zylinder
        }

        top = (getHeight() - n/2) >> 1;
        left = ( getWidth()-n) >> 1;

        if (_bHaus){
            g.setColor(Color.BLUE);
            g.drawRect(left, top, n,n);
        }

        // Dach
        if (_bDach){
            Graphics2D g2 = (Graphics2D) g;
            float [] dashs = {10.0f, 15.0f};  // Länge Strich , Pause
            // BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase)
            //  1.0f = miterlimit
            g2.setStroke( new BasicStroke(3.0f,  BasicStroke.CAP_BUTT,   BasicStroke.JOIN_MITER, 1.0f, dashs, 0.0f ) );

            g.setColor(Color.BLUE);
            g.drawLine(left,top,            left+n/2, top-n/2 );
            g.drawLine(left+n/2, top-n/2,   left+n, top);
        }

        // Diagonale
        if (_Diagonale){
            Graphics2D g3 = (Graphics2D) g;
            float [] dashs3 = {10.0f, 5.0f, 4.0f, 5.0f};  // Länge Strich , Pause
            // BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase)
            //  1.0f = miterlimit
            g3.setStroke( new BasicStroke(3.0f,  BasicStroke.CAP_BUTT,   BasicStroke.JOIN_MITER, 1.0f, dashs3, 0.0f ) );
            g.setColor( new Color(255,0,255)  );
            g.drawLine(left,top,            left+n, top+n );
            g.drawLine(left, top+n,         left+n, top);
        }

        // Farbiges Dach
        if (_bZiegel){
           g.setColor(Color.RED);
           int x[] = new int[3];
           int y[] = new int[3];
            x[0] = left;   y[0] = top;
            x[1] = left+n/2;   y[1] = top-n/2;
            x[2] = left+n;   y[2] = top;
          g.fillPolygon(x,y,3);
        }


      // Interne Kreise
      if (_bKreise){
          Graphics2D g4 = (Graphics2D) g;
          g4.setStroke( new BasicStroke(2.0f) );
          g.setColor(Color.CYAN);
          g.drawOval(left,top+n/4,n/2, n/2);
          g.drawOval(left+n/2,top+n/4,n/2, n/2);
          g.setColor(Color.GREEN);
          g.drawOval(left+n/4,top,n/2, n/2);
          g.drawOval(left+n/4,top+n/2,n/2, n/2);
      }


      // Text
      if (_bSchrift){
          g.setColor(new Color(200,200,00));
          String s = "x: "+Integer.toString(left)+"  "+"y: "+Integer.toString(top);
          g.drawString(s, left-70,top);

          s = "x: "+Integer.toString(left)+"  "+"y: "+Integer.toString(top+n);
          g.drawString(s, left-70,top+n);

          s = "x: "+Integer.toString(left+n)+"  "+"y: "+Integer.toString(top);
          g.drawString(s, left+n+10,top);

          s = "x: "+Integer.toString(left+n)+"  "+"y: "+Integer.toString(top+n);
          g.drawString(s, left+n+10,top+n);
      }



    }  // paint

    
    public void setHaus(boolean bHaus) {
        _bHaus = bHaus;
    }
    
    public void setDach(boolean bDach) {
        _bDach = bDach;
    }

    
    public void setZiegel(boolean bZiegel) {
        _bZiegel = bZiegel;
    }

    public void setDiagonale(boolean bDiagonale) {
        _Diagonale = bDiagonale;
    }
    
    public void setKreise(boolean bKreise) {
        _bKreise = bKreise;
    }

    
    public void setSchrift(boolean bSchrift) {
        _bSchrift = bSchrift;
    }
    
    
}  // CCanvas
        
        
