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

public class Dialog_2D extends JFrame {


  JLabel LWidth = new JLabel("Breite");
  JTextField TWidth = new JTextField();

  JButton BnDraw= new JButton("Draw");

  CCanvas _canvas = new CCanvas();


  public Dialog_2D(String caption) {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setTitle(caption);
    setGUI();
  }

  private void setGUI() {
    this.getContentPane().setLayout( new GridBagLayout() );


    this.getContentPane().add(LWidth,
        new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.NONE,
            new Insets(20,20,0,20), 0, 0));

    this.getContentPane().add(TWidth,
        new GridBagConstraints(1, 0, 1, 1, 100.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.HORIZONTAL,
            new Insets(20,20,0,20), 0, 0));

    JPanel panelBn = new JPanel();
    panelBn.setLayout( new GridBagLayout() );

    this.getContentPane().add(panelBn,
        new GridBagConstraints(0, 1, 2, 1, 100.0, 0.0
            ,GridBagConstraints.CENTER,
            GridBagConstraints.HORIZONTAL,
            new Insets(20,20,20,20), 0, 0));

    panelBn.add(BnDraw);
    BnDraw.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        BnDraw_Click();
      }
    });


    this.getContentPane().add(_canvas,
        new GridBagConstraints(0, 2, 2, 1, 100.0, 100.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.BOTH,
            new Insets(0,20,30,20), 0, 0));

  }

   // alte Version, Problem des Updates mit Canvas
  private void BBnDraw_Click() {
    //_canvas.LineWidth = 3;
    //_canvas.resize( _canvas.getWidth(), _canvas.getHeight() );
//    _canvas.paintImmediately(0,0,_canvas.getWidth(), _canvas.getHeight() );

//    this.setBounds( this.getX(), this.getY(), this.getWidth(), this.getHeight()  );
  }

 private void BnDraw_Click() {
    String sNumber = TWidth.getText();
    if ( CBasis.DVal(sNumber) ) {
      _canvas.LineWidth = (float) CBasis.getDValResult();
      //_canvas.update( _canvas.getGraphics() );
      //this.paint( this.getGraphics() );
      _canvas.updateUI();
    }
    else  {
      System.out.println("Fehlerhafte Zahl");
    }

  }



    public static void main(String args[]) {
      Dialog_2D f = new Dialog_2D("Dialog_2D");
     	f.setSize(400,400);
    	f.setVisible(true);
    }


}

class CCanvas extends JPanel {

  public float LineWidth=1f;

  public void paint (Graphics g) {
    System.out.println("paint1");
    int x = 80;
    Rectangle r = new Rectangle();
    g.getClipBounds(r);

    Graphics2D g2 = (Graphics2D) g;
    float [] dashs = {40.0f, 5.0f};  // Länge Strich , Pause
    // BasicStroke(float width, int cap, int join, float miterlimit,
    //             float[] dash, float dash_phase)
    //  1.0f = miterlimit

    g.setColor(Color.WHITE);
    g.fillRect(0,0, getWidth(), getHeight() );
    System.out.println("LineWidth: "+LineWidth);
    g2.setStroke( new BasicStroke(LineWidth,  BasicStroke.CAP_BUTT,
          BasicStroke.JOIN_MITER, 1.0f, dashs, 0.0f ) );

    if (LineWidth<2)
      g.setColor(Color.RED);
    else
      g.setColor(Color.BLUE);
    for (int i=10; i< r.getWidth()-10; i++) {
      x = (int) (6.0*Math.random());
      if (x==2)
      g.drawLine(i,20,i,(int) r.getHeight()-10 );
      //x += 1+ 3*Math.random();
    } // for
    //g2.dispose();
  }  // paint


}  // CCanvas



class cCCanvas extends JPanel {

  public float LineWidth=1f;

  public void paint (Graphics g) {
    int x;

    System.out.println("paint");

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

    Rectangle r = new Rectangle();
    g.getClipBounds(r);

    g.setColor(Color.black);

/*
    Graphics2D g2 = (Graphics2D) g;
    float [] dashs = {40.0f, 5.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(LineWidth,  BasicStroke.CAP_BUTT,
          BasicStroke.JOIN_MITER, 1.0f, dashs, 0.0f ) );
*/
    x=80;
    for (int i=10; i< r.getWidth()-10; i++) {
      x = (int) (6.0*Math.random());
      if (x==2)
        g.drawLine(i,20,i,(int) r.getHeight()-10 );
      //x += 1+ 3*Math.random();
    }
  }

}



class CBasis {
    // Konstanten
    public static final int C_LEER = 1;
    public static final int C_KEINEZAHL = 2;
    public static final int C_ZUKLEIN = 3;
    public static final int C_ZUGROSS = 4;

      // Konstanten für den Error Code als String
    public static final String SZ_LEER = "Leerer Text";
    public static final String SZ_KEINEZAHL = "Konvertierung schlug fehl";
    public static final String SZ_ZUKLEIN = "Für den Bereich zu klein";
    public static final String SZ_ZUGROSS = "Für den Bereich zu groß";


    private static double dblVal;       // speichert das Ergebnis
    private static int ValError = 0;   // speichert den Konvertierungfehler


/**
    * DVal
    * description: überprüft, ob der String eine Double-Zahl ist
    * Verwendet eine interne Variable zum Speichern des Wertes
    * @see IVal, LVal, getErrorCode
    *
    * @version     1.0  07.07.2002
    * @author      Michael Wilhelm HS Harz, FB AI
    * @param String sText
    * @return boolean
    * @exception keine
    */
    public static boolean DVal(String sText) {
        // erläuterung von iIVal = Integer.valueOf(szText).intValue();
        boolean result = true;
        String s;
        s = new String(sText); // neues Objekt
        s.trim();
        if (s.length() == 0) {
            ValError = C_LEER;
            dblVal = 0;
            result = false;
        }
        else {
            try {
                dblVal = Double.valueOf(sText).doubleValue();
                ValError = 0;
            }
            catch (NumberFormatException e) {
                ValError = C_KEINEZAHL;
                dblVal = 0;
                result = false;
            }
        }
        return result;
    } // DVal


    /**
    * description: gibt den konvertierten Wert aus
    * @see IVal, DVal, LVal, getErrorCode
    *
    * @version     1.0  07.07.2002
    * @author Michael Wilhelm HS Harz, FB AI
    * @param keinen
    * @return double
    * @exception keine
    * @use gibt den Wert der Konvertierung aus
    * @see #getIValError
    * @see #setLValError
    * @see #setDValError
    */
    public static double getDValResult() {
       return dblVal;
    }

}  // CBasis