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


// Beispiel mit einer Linie mit erweiterten Eigenschaften
// Strickdicke
// Muster
// Ende
// Join

public class Dialog_Line_2D extends JFrame {


  JLabel LWidth = new JLabel("Strickdicke");
  JTextField TWidth = new JTextField("12.5");

  JLabel LEndStyles = new JLabel("EndStyles");
  JComboBox cbEndStyles;

  JLabel LLineStyles = new JLabel("LineStyles");
  JComboBox cbLineStyles;

  JLabel LJoinStyles = new JLabel("JoinStyles");
  JComboBox cbJoinStyles;

  JLabel LMiterSize = new JLabel("MiterSize");
  JTextField TMiterSize = new JTextField("7");

  JButton BnDraw= new JButton("Draw");

  CCanvas _canvas = new CCanvas();


  public Dialog_Line_2D(String caption) {
    setTitle(caption);
    setSize(600,600);
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

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

    JPanel pnTop = new JPanel();
    pnTop.setBackground( Color.lightGray );
    pnTop.setLayout( new GridBagLayout() );

    this.getContentPane().add(pnTop, BorderLayout.NORTH);

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

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


    String myArray1[] = new String[3];
    myArray1[0] = "CAP_BUTT";
    myArray1[1] = "CAP_ROUND";
    myArray1[2] = "CAP_SQUARE";
    cbEndStyles = new JComboBox(myArray1);
    cbEndStyles.setSelectedIndex(0);

    pnTop.add(LEndStyles,
        new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.NONE,
            new Insets(20,20,20,20), 0, 0));

    pnTop.add(cbEndStyles,
        new GridBagConstraints(3, 0, 1, 1, 100.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.HORIZONTAL,
            new Insets(20,20,20,20), 0, 0));


    // -------------------------------
    String myArray2[] = new String[7];
    myArray2[0] = "solid";
    myArray2[1] = "dots";
    myArray2[2] = "big dots";
    myArray2[3] = "dash";
    myArray2[4] = "long dash";
    myArray2[5] = "dot dash";
    myArray2[6] = "dot dot dash";
    cbLineStyles = new JComboBox(myArray2);
    cbLineStyles.setSelectedIndex(0);

    pnTop.add(LLineStyles,
        new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.NONE,
            new Insets(20,20,20,20), 0, 0));

    pnTop.add(cbLineStyles,
        new GridBagConstraints(1, 2, 1, 1, 100.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.HORIZONTAL,
            new Insets(20,20,20,20), 0, 0));


    // -------------------------------
    String myArray3[] = new String[3];
    myArray3[0] = "JOIN_BEVEL";
    myArray3[1] = "JOIN_MITER";
    myArray3[2] = "JOIN_ROUND";
    cbJoinStyles = new JComboBox(myArray3);
    cbJoinStyles.setSelectedIndex(0);

    pnTop.add(LJoinStyles,
        new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.NONE,
            new Insets(20,20,20,20), 0, 0));

    pnTop.add(cbJoinStyles,
        new GridBagConstraints(3, 2, 1, 1, 100.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.HORIZONTAL,
            new Insets(20,20,20,20), 0, 0));


    // -------------------------------


    pnTop.add(LMiterSize,
        new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.NONE,
            new Insets(20,20,20,20), 0, 0));

    pnTop.add(TMiterSize,
        new GridBagConstraints(1, 3, 1, 1, 100.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.HORIZONTAL,
            new Insets(20,20,20,20), 0, 0));



   // ------------------------------

    JPanel panelBn = new JPanel();
    panelBn.setLayout( new GridBagLayout() );
    panelBn.add(BnDraw);
    pnTop.add(panelBn,
        new GridBagConstraints(0, 4, 4, 1, 100.0, 0.0
            ,GridBagConstraints.NORTH,
            GridBagConstraints.HORIZONTAL,
            new Insets(20,20,20,20), 0, 0));



    BnDraw.addActionListener(new java.awt.event.ActionListener() {

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


    this.getContentPane().add(_canvas, BorderLayout.CENTER);
  }

  private void BnDraw_Click() {
    String sNumber = TWidth.getText();
    if ( CBasis.DVal(sNumber) ) {
      _canvas.LineWidth = (float) CBasis.getDValResult();
    }

    if ( CBasis.DVal( TMiterSize.getText() ) ) {
       System.out.println("Miter: "+(float) CBasis.getDValResult()  );
      _canvas.MiterSize = (float) CBasis.getDValResult();
    }


    switch ( cbEndStyles.getSelectedIndex() ) {
      case 0: _canvas.EndStyles = BasicStroke.CAP_BUTT;
              break;
      case 1: _canvas.EndStyles = BasicStroke.CAP_ROUND;
              break;
      case 2: _canvas.EndStyles = BasicStroke.CAP_SQUARE;
              break;
    }
    switch ( cbJoinStyles.getSelectedIndex() ) {
      case 0: _canvas.JoinStyles = BasicStroke.JOIN_BEVEL;
              break;
      case 1: _canvas.JoinStyles = BasicStroke.JOIN_MITER;
              break;
      case 2: _canvas.JoinStyles = BasicStroke.JOIN_ROUND;
              break;
    }
    _canvas.LineStyle = cbLineStyles.getSelectedIndex();

    _canvas.updateUI();

  }


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


}




class CCanvas extends JPanel {

  public float LineWidth=1f;
  public float MiterSize=4.0f;
  public int EndStyles=BasicStroke.CAP_BUTT;  //CAP_BUTT  CAP_ROUND  CAP_SQUARE
  public int JoinStyles=BasicStroke.JOIN_BEVEL;  //BasicStroke.JOIN_MITER  BasicStroke.JOIN_ROUND
  public int LineStyle;


  public void paint (Graphics g) {
    //int x = 80;

    System.out.println("paint");

    Rectangle r = new Rectangle();  // double wert
    g.getClipBounds(r);
        // Löschen des Bildschirms
    g.setColor(Color.white);
    g.fillRect(0,0, (int)r.getWidth(), (int)r.getHeight() );

    // holt die Abmessungen des Canvas

    g.setColor(Color.black);

    Graphics2D g2 = (Graphics2D) g;
    // BasicStroke(float width, int cap, int join,
    //             float miterlimit,
    //             float[] dash, float dash_phase)

    // End-Styles BasicStroke.CAP_BUTT  CAP_ROUND  CAP_SQUARE
    switch (LineStyle) {
      case 0: g2.setStroke( new BasicStroke(LineWidth,  EndStyles,
                  JoinStyles, MiterSize) ); //, dashs, 0.0f ) );
              break;
      case 1: float [] dashs1 = {5.0f, 5.0f};  // dots
              g2.setStroke( new BasicStroke(LineWidth,  EndStyles,
                  JoinStyles, MiterSize, dashs1, 0.0f ) );
              break;
      case 2: float [] dashs2 = {15.0f, 15.0f};  // big dots
              g2.setStroke( new BasicStroke(LineWidth,  EndStyles,
                  JoinStyles, MiterSize, dashs2, 0.0f ) );
              break;
      case 3: float [] dashs3 = {30.0f, 5.0f};  // dash
              g2.setStroke( new BasicStroke(LineWidth,  EndStyles,
                  JoinStyles, MiterSize, dashs3, 0.0f ) );
              break;
      case 4: float [] dashs4 = {70.0f, 10.0f};  // long dash
              g2.setStroke( new BasicStroke(LineWidth,  EndStyles,
                  JoinStyles, MiterSize, dashs4, 0.0f ) );
              break;
      case 5: float [] dashs5 = {5.0f, 10.0f, 70.0f, 10.0f};  // dot dash
              g2.setStroke( new BasicStroke(LineWidth,  EndStyles,
                  JoinStyles, MiterSize, dashs5, 0.0f ) );
              break;
      case 6: float [] dashs6 = {5.0f, 10.0f,5.0f, 10.0f, 70.0f, 10.0f};  // dot dot dash
              g2.setStroke( new BasicStroke(LineWidth,  EndStyles,
                  JoinStyles, MiterSize, dashs6, 0.0f ) );
              break;
    }

    int[] x = new int[5];
    int[] y = new int[5];
    x[0] = 20;
    x[1] = (int)(r.getWidth()*0.5);
    x[2] = (int)(r.getWidth()*0.8);
    x[3] = (int)(r.getWidth()*0.35);
    x[4] = (int)(r.getWidth()*0.9);

    y[0] = 20;
    y[1] = (int) (r.getHeight()-40) ;
    y[2] = (int) (r.getHeight()*0.4);
    y[3] = (int) (r.getHeight()*0.1);
    y[4] = (int) (r.getHeight()*0.2);
    g.drawPolyline(x,y,5);
  }

}

class CCCanvas extends Canvas {

  public float LineWidth=1f;

  public void paint (Graphics g) {
     reDraw();
  }

  public void reDraw() {
    Graphics g = this.getGraphics();
    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
    g2.setStroke( new BasicStroke(LineWidth,  BasicStroke.CAP_BUTT,
          BasicStroke.JOIN_MITER, 1.0f, dashs, 0.0f ) );

    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 {
    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 int intVal;          // speichert das Ergebnis
    private static long longVal;        // speichert das Ergebnis
    private static double dblVal;       // speichert das Ergebnis

    private static int ValError = 0;   // speichert den Konvertierungfehler


    /**
    * description: überprüft, ob der String eine Integerzahl ist
    * Verwendet eine interne Variable zum Speichern des Wertes
    * @see DVal, 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 IVal(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;
            intVal = 0;
            result = false;
        }
        else {
            try {
                intVal = Integer.valueOf(sText).intValue();
                ValError = 0;
            }
            catch (NumberFormatException e) {
                // Check ob die "Zahl" für den Integerbereich zu klein oder zu groß
                // System.err.println(e);
                try {
                    dblVal = Double.valueOf(sText).doubleValue();
                    if ( dblVal<Integer.MIN_VALUE) {
                      ValError = C_ZUKLEIN;
                    }
                    if ( dblVal>Integer.MAX_VALUE) {
                      ValError = C_ZUGROSS;
                    }
                }
                catch (NumberFormatException e2) {
                    // Hier könnte noch eine genauere Abfrage integriert werde,
                    // ob die Zahl nicht zu groß für den int-Bereich ist
                    // System.err.println(e);
                    ValError = C_KEINEZAHL;
                    intVal = 0;
                    result = false;
                }
                intVal = 0;
                result = false;
            }
        }
        return result;
    } // IVal


    /**
    * 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 String sText
    * @return String
    * @exception keine
    * @use gibt den Wert der Konvertierung aus
    * @see #setIValError
    * @see #setLValError
    * @see #setDValError
    */
    public static int getIValResult() {
       return intVal;
    }



    /**
    * 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 {
                sText = sText.replace(',','.');
                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;
    }


}

    
