import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 23.02.2006
  * @author
  */

public class Serienaddierer extends Applet {
  // Anfang Variablen
  private Label label1 = new Label();
  private Label label2 = new Label();
  private TextField b1 = new TextField();
  private TextField aus = new TextField();
  private Button takt = new Button();
  private Button reset = new Button();
  int i=-1,s=0, cb1,cb2;
  char rein,raus;
  private Label label3 = new Label();
  private TextField b2 = new TextField();
  private TextField state = new TextField();
  private Label label4 = new Label();
  // Ende Variablen

  public void init() {
    Panel cp = new Panel(null);
    cp.setBounds(0, 0, 529, 229);
    add(cp);
    // Anfang Komponenten

    label1.setBounds(16, 16, 51, 24);
    label1.setText("Bit 1");
    label1.setFont (new Font("MS Sans Serif", Font.PLAIN, 19));
    cp.add(label1);
    label2.setBounds(16, 96, 71, 24);
    label2.setText("Summe:");
    label2.setFont (new Font("MS Sans Serif", Font.PLAIN, 19));
    cp.add(label2);
    b1.setBounds(112, 16, 377, 32);
    b1.setFont (new Font("MS Sans Serif", Font.PLAIN, 19));
    b1.setText("01100100110");
    cp.add(b1);
    aus.setBounds(112, 96, 377, 32);
    aus.setFont (new Font("MS Sans Serif", Font.PLAIN, 19));
    aus.setText("");
    cp.add(aus);
    takt.setBounds(176, 136, 75, 33);
    takt.setFont (new Font("MS Sans Serif", Font.PLAIN, 19));
    takt.setLabel("Takt");
    cp.add(takt);
    takt.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        taktActionPerformed(evt);
      }
    });

    reset.setBounds(288, 136, 75, 33);
    reset.setFont (new Font("MS Sans Serif", Font.PLAIN, 19));
    reset.setLabel("Reset");
    cp.add(reset);
    reset.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        resetActionPerformed(evt);
      }
    });

    label3.setBounds(16, 56, 51, 24);
    label3.setText("Bit 2");
    label3.setFont (new Font("MS Sans Serif", Font.PLAIN, 19));
    cp.add(label3);
    b2.setBounds(112, 56, 377, 32);
    b2.setFont (new Font("MS Sans Serif", Font.PLAIN, 19));
    b2.setText("01010111000");
    cp.add(b2);
    state.setBounds(112, 136, 33, 32);
    state.setFont (new Font("MS Sans Serif", Font.PLAIN, 19));
    state.setText("S"+s);
    cp.add(state);
    label4.setBounds(16, 136, 73, 24);
    label4.setText("Zustand:");
    label4.setFont (new Font("MS Sans Serif", Font.PLAIN, 19));
    cp.add(label4);
    // Ende Komponenten

  }
  // Anfang Ereignisprozeduren
  public void taktActionPerformed(ActionEvent evt) {
    i++;
    if( i < b1.getText().length() )
    {
      cb1 = (int)b1.getText().charAt(i)-48;
      cb2 = (int)b2.getText().charAt(i)-48;
      cb1=cb1+cb2;
      if (cb1==0) rein='N';
      if (cb1==1&&cb2==1) rein='E';
      if (cb1==1&&cb2==0) rein='A';
      if (cb1==2) rein='Z';
      raus = g(s,rein);
      aus.setText(aus.getText()+raus);
      s = u(s,rein);
      state.setText("S"+s);
    }


  }

  public void resetActionPerformed(ActionEvent evt) {
         neu();
  }

  // Ende Ereignisprozeduren
  private void neu(){
      s = 0;
      i = -1;
      b1.setText("");
      b2.setText("");
      aus.setText("");
  }
  public int u(int s, char e)  {
    switch(s)    {
      case 0: switch(e) {case 'N': return 0; case 'E':return 0;
      case 'A': return 0;case 'Z': return 1;}
      case 1: switch(e) {case 'N': return 0; case 'E':return 1;
      case 'A': return 1;case 'Z': return 1;}
      default: return 99;
    }
  }
  public char g(int s, char e)  {
    switch(s){
      case 0: switch(e) {case 'N': return '0'; case 'E':return '1';
      case 'A': return '1';case 'Z': return '0';}
      case 1: switch(e) {case 'N': return '1'; case 'E':return '0';
      case 'A': return '0';case 'Z': return '1';}
      default: return ' ';
    }
  }


}
