import java.awt.*;
import java.applet.*;
import java.awt.event.*;

//-------------Version Oktober 2002-----------------------------
//-------------bearbeitet Februar 2005--------------------------
//-------------(C) Ulli ----------------------------------------

public class HyperbApp extends Applet
{
  Panel panA=new Panel();
  Panel panU=new Panel();
  Panel panO=new Panel();  
  Panel panM=new Panel();
  TCanvas drawC=new TCanvas(this);
  Button drawB=new Button("draw");
  Button clearB=new Button("clear");
  Button stopB=new Button("stop");
  Checkbox cbk=new Checkbox("show construction",true);
  Checkbox cb1=new Checkbox("AB",true); 
  Checkbox cb2=new Checkbox("AC",true);
  Checkbox cb3=new Checkbox("BC",true);  
  Label hinweis=new Label("move circles"); 
  Label kL=new Label(" k:");
  Label nL=new Label(" n:"); 
  Label mL=new Label(" m:");
  Label conkL=new Label(" generator 1:");
  Label connL=new Label(" generator 2:");
  Label conmL=new Label(" generator 3:");
  Label dotsL=new Label(" dots:");
  Label iterL=new Label(" iterations:");
  Color bgcolor=new Color(10,10,100);
  Color tbgcolor=Color.black;
  Color buttonC=new Color(200,200,200);
  TextField kF=new TextField(3);
  TextField nF=new TextField(3);
  TextField mF=new TextField(3);
  TextField conkF=new TextField(8);
  TextField connF=new TextField(8);
  TextField conmF=new TextField(8);
  TextField dotsF=new TextField(7);
  TextField iterF=new TextField(5);  
  Waechter ew=new Waechter(); 
  HypMover A,B;
  HypConstruction hypCon; 
  int k,n,m;
  String ek,en,em;
  int dots,iter;   
  boolean drawn;
  Font tfont=new Font("SansSerif",Font.PLAIN,11);
  
  public void init()
  {
    drawC.setSize(540,378);
    drawC.setBackground(tbgcolor);
    A=new HypMover(this,hinweis,drawC,265,175,"move circles","captured A");    
    B=new HypMover(this,hinweis,drawC,325,205,"move circles","captured B");
    A.setBackcolor(tbgcolor); A.setDrawcolor(Color.yellow);      
    A.setShowpoint(true);
    B.setBackcolor(tbgcolor); B.setDrawcolor(Color.yellow);      
    B.setShowpoint(true);
    hypCon=new HypConstruction(drawC,hinweis,6,A,B);
    
    //die drawWorld-Methode ist in die run-Methode des hypCon-
    //Threads eingeschlossen. Start und Stopp des Zeichnens wird
    //über hypCon.setWorld(true oder false) gesteuert.
    //true startet die Zeichnung, false lässt sie abbrechen.
    
    hypCon.start();
    
    ek=new String();
    en=new String();
    em=new String();
        
    // Initialisierungsmethode der Zeichenmaschine
    
    kF.addActionListener(ew);
    nF.addActionListener(ew);
    mF.addActionListener(ew);
    conkF.addActionListener(ew);
    connF.addActionListener(ew);
    conmF.addActionListener(ew);
    dotsF.addActionListener(ew);
    iterF.addActionListener(ew);
    
    kF.setText("4");
    nF.setText("3");
    mF.setText("3");
    conkF.setText("1");
    connF.setText("2");
    conmF.setText("3");
    dotsF.setText("500");
    iterF.setText("100");
    
    kF.setBackground(Color.white);
    nF.setBackground(Color.white);
    mF.setBackground(Color.white);
    conkF.setBackground(Color.white);
    connF.setBackground(Color.white);
    conmF.setBackground(Color.white);
    dotsF.setBackground(Color.white);
    iterF.setBackground(Color.white);    
    
    setBackground(bgcolor);
    hinweis.setForeground(Color.white);
    hinweis.setFont(tfont);
    drawB.addActionListener(ew);
    clearB.addActionListener(ew);
    stopB.addActionListener(ew);
    drawB.setBackground(buttonC);
    clearB.setBackground(buttonC);
    stopB.setBackground(buttonC);
    cbk.addItemListener(ew);
    
    kL.setForeground(Color.white);
    kL.setBackground(bgcolor);
    kL.setFont(tfont);
    nL.setForeground(Color.white);
    nL.setBackground(bgcolor); 
    nL.setFont(tfont);
    mL.setForeground(Color.white);
    mL.setBackground(bgcolor);
    mL.setFont(tfont);
    conkL.setForeground(Color.white);
    conkL.setBackground(bgcolor);
    conkL.setFont(tfont);
    connL.setForeground(Color.white);
    connL.setBackground(bgcolor);
    connL.setFont(tfont);
    conmL.setForeground(Color.white);
    conmL.setBackground(bgcolor); 
    conmL.setFont(tfont);
    dotsL.setForeground(Color.white);
    dotsL.setBackground(bgcolor);
    dotsL.setFont(tfont);
    iterL.setForeground(Color.white);
    iterL.setBackground(bgcolor);
    iterL.setFont(tfont);
    cbk.setForeground(Color.white);    
    cbk.setBackground(bgcolor);
    cbk.setFont(tfont);
    cb1.setForeground(Color.white);
    cb1.setBackground(bgcolor);
    cb1.setFont(tfont);
    cb2.setForeground(Color.white);
    cb2.setBackground(bgcolor);
    cb2.setFont(tfont);
    cb3.setForeground(Color.white);
    cb3.setBackground(bgcolor);
    cb3.setFont(tfont);
        
    panO.setLayout(new BorderLayout());    
    panO.add(drawC); 
    panM.add(hinweis); panM.add(drawB); 
    panM.add(clearB); panM.add(stopB);
    panM.add(cbk);
    panM.add(cb1); panM.add(cb2); panM.add(cb3);
       
    panA.add(kL); panA.add(kF);
    panA.add(nL); panA.add(nF);
    panA.add(mL); panA.add(mF);
    panA.add(dotsL); panA.add(dotsF);
    panA.add(iterL); panA.add(iterF);
    panU.add(conkL); panU.add(conkF);
    panU.add(connL); panU.add(connF);         
    panU.add(conmL); panU.add(conmF);     
    panO.add(panM,"South");    
    add(panO); 
    add(panA); add(panU);         
  }   
  
  public void start()
  {
    drawn=false;
    paint(getGraphics());
  }
  
  public void update(Graphics g)
  {
    // Konstruktionsmethode der Maschine
    if (!drawn)
    {
      drawC.clear(tbgcolor);    
      hypCon.draw();    
    }  
  }
  
  public void paint(Graphics g)
  {
    update(g);    
  }
     
  double getdValue(TextField tF)
  {
    double z=0;
    try
    {
      z=Double.valueOf(tF.getText()).doubleValue();      
    }
    catch (NumberFormatException e){}
    return z;
  }
  
  int getiValue(TextField tF)
  {
    int z=0;
    try
    {
      z=Integer.valueOf(tF.getText()).intValue();      
    }
    catch (NumberFormatException e){}
    return z;
  }
  
  void newfigure()
  {
    k=getiValue(kF);
    n=getiValue(nF);
    m=getiValue(mF);
    dots=getiValue(dotsF);
    iter=getiValue(iterF);    
    ek=conkF.getText();
    en=connF.getText(); 
    em=conmF.getText();
    boolean b1,b2,b3;
    b1=cb1.getState(); b2=cb2.getState(); b3=cb3.getState();    
    //Parameter an die Zeichenmaschine übergeben
    hypCon.setParams(k,n,m,b1,b2,b3,ek,en,em,dots,iter);    
  }  
  
  class Waechter implements ActionListener, ItemListener
  {
    public void actionPerformed(ActionEvent e)
    {
      if (e.getSource()==clearB)
      {
        newfigure();
        repaint();
        drawn=false;            
      }      
      if (e.getSource()==stopB)
      {
        hypCon.setWorld(false);            
      } 
      if (e.getSource()==drawB)
      {
        newfigure(); 
        //Zeichenmethode der Maschine
        hypCon.setWorld(true);
        drawn=true;          
      } 
      if ((e.getSource()!=drawB)&(e.getSource()!=stopB)
           &(e.getSource()!=clearB))
      {
        newfigure();             
      }     
    }
    
    public void itemStateChanged(ItemEvent e)
    {
      hypCon.setVisible(cbk.getState());
      drawn=false;
      repaint(); 
    }    
  }  

}
