/**
  *
  * <Beschreibung>
  *
  * @version 1.0 vom 16.01.2004
  * @author
  */

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

public class CAApplet extends JApplet implements ActionListener {
  // Anfang Variablen
  //Appletgroesse Width=620 Height=450>
    private int width = 600, height = 400;
   //Appletgroesse Width=219 Height=320>
   //private int width = 300, height = 180; 
    private int rand=1;
    private int evSchritte=1;
    private byte[] band;
    private int nummer = 89;
    private Regel regel; 
    private boolean stochastisch = false;
    private Container c;
    private JPanel canvas, control;
    private JLabel jlInfo1;
    private JButton jbStart,jbPlus,jbMinus;   
    private JTextField tfEingabe;
    private JCheckBox jcStochastisch;
  // Ende Variablen

  public void init () {
        this.regel = new Regel(nummer);   
        rand = height; // - width/2;
        if (rand < 1) rand =1;
    // Anfang Komponenten
        c = getContentPane();
        c.setLayout(new BorderLayout());
        canvas = new JPanel();
        c.add("Center",canvas);
        control = new JPanel();
        control.setLayout(new FlowLayout());
        c.add("South", control);
        //jlInfo1 = new JLabel("regel (0 <= nummer <= 255): ");
        jlInfo1 = new JLabel("Regel: ");
        control.add(jlInfo1);
        tfEingabe = new JTextField();
        tfEingabe.setColumns(3);
        control.add(tfEingabe);
        tfEingabe.setText(""+nummer);
        jbStart = new JButton("zeige");
        control.add(jbStart);
        jbPlus = new JButton("naechste");
        control.add(jbPlus);
        jbMinus = new JButton("vorige");
        control.add(jbMinus);
        jcStochastisch = new JCheckBox("stochastisch", false);
        control.add(jcStochastisch);
        
        jbStart.addActionListener(this);
        tfEingabe.addActionListener(this);
        jbPlus.addActionListener(this);
        jbMinus.addActionListener(this);
        jcStochastisch.addActionListener(this);
    // Ende Komponenten

  }
      public void paint (Graphics g){
         c.paintComponents(g);
         zeigeRegel(g, regel);
        setBackground(Color.white);
        Dimension size = getSize();
        evSchritte = size.height-70;
        rand = evSchritte;
        width = size.width-20; height = size.height-40; rand = height;
        g.setColor(getBackground());
        g.fillRect(4,25,size.width-12, size.height-60);
        g.setColor(Color.black);
        initBand();
        for (int i=0; i < evSchritte; i++){
          for (int k = rand; k < rand+width; k++){
            if (band[k] == 1){
             g.fillRect(k-rand+8,i+30,1,1);
            }
       }// for k
       byte[] band1 = regel.wendeAn(band);
       band = band1;
       }// for i
      //}

     }

        private void initBand(){
        int size = 2*rand+width;
        band = new byte[size];
        if(stochastisch)
          for(int k=0; k < size; k++){
           if(Math.random()<0.5) band[k]=1;
           else band[k]=0;
        }
        else // nicht stochastisch
          for(int k=0; k < size; k++){
             if(k == size/2) band[k]=1;
             else band[k]=0;
         }
    }

      private void zeigeRegel(Graphics g, Regel regel){
             String[] muster = regel.liesMuster();
             byte[] ersetzung = regel.liesErsetzungen();
             int stelle=0;
             for (int anz=0; anz < muster.length; anz++){
              for (int k = 0; k < 3; k++){
                g.drawRect(5*(2+4*stelle+k),5+0,5,5);
                if (muster[anz].charAt(k) == '1'){
                  g.fillRect(5*(2+4*stelle+k),5+0,5,5);
                }
            }// for k
            g.drawRect(5*(2+4*stelle+1),5*2+2,5,5);
            if (ersetzung[anz] == 1){
                  g.fillRect(5*(2+4*stelle+1),5*2+2,5,5);
                }
                stelle++;
        }
    }
  
  
  // Anfang Ereignisprozeduren
  public void actionPerformed (ActionEvent e){
   Object obj = e.getSource();
   int nr=nummer;
   if (obj == tfEingabe || obj == jbStart){
       String s = tfEingabe.getText();
       try {
           nr = Integer.parseInt(s);
           if (nr>=0 && nr <= 255) {
             nummer = nr;
           }
       } catch (Exception exc){}
       tfEingabe.setText(""+nummer);
   }
   else if (obj == jbPlus){
     nummer = (nummer +1)%256;
     tfEingabe.setText(""+nummer);
   }
   else if (obj == jbMinus){
     nummer--;
     if (nummer < 0) nummer = 255;
     tfEingabe.setText(""+nummer);
   }
   else if (obj == jcStochastisch){
     stochastisch = jcStochastisch.isSelected();
   }
      regel.waehleRegel(nummer);
      repaint();
  }
  // Ende Ereignisprozeduren

}