import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 09.03.2006
  * @author
  */

public class FlipFlop extends Applet {
  // Anfang Variablen
  private Button S = new Button();
  private Button R = new Button();
  private Button W = new Button();
  private Button B = new Button();
  private Label Zustand = new Label();
  private TextField state = new TextField();
  private Label Ausgabe = new Label();
  private TextField aus = new TextField();
  private Label ueberschrift = new Label();
  int anzE=4; int anzA=2; int anzS=2; // Anzahl der Eingabezeichen, Ausgabezeichen und Zustände
  int e=0;  //aktuellles Eingabezeichen
  int a=0;  //aktuelles Ausgabezeichen
  int s=0;  //aktueller Zustand
  String [] E = new String[anzE]; //Eingabezeichen
  String [] A = new String[anzA]; //Ausgabezeichen
  String [] sS = new String[anzS];
  int [] [] u= new int [anzS] [anzE]; //Überführungsmatrix
  int [] [] g= new int [anzS] [anzE]; //Ausgabematrix
  // Ende Variablen

  public void init() {
    Panel cp = new Panel(null);
    cp.setBounds(0, 0, 357, 172);
    add(cp);
    // Anfang Komponenten

    S.setBounds(40, 32, 35, 25);
    S.setLabel("S");
    cp.add(S);
    S.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        SActionPerformed(evt);
      }
    });

    R.setBounds(112, 32, 27, 25);
    R.setLabel("R");
    cp.add(R);
    R.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        RActionPerformed(evt);
      }
    });

    W.setBounds(176, 32, 35, 25);
    W.setLabel("W");
    cp.add(W);
    W.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        WActionPerformed(evt);
      }
    });

    B.setBounds(248, 32, 27, 25);
    B.setLabel("B");
    cp.add(B);
    B.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        BActionPerformed(evt);
      }
    });

    Zustand.setBounds(40, 88, 56, 16);
    Zustand.setText("Zustand");
    Zustand.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
    cp.add(Zustand);
    state.setBounds(104, 80, 41, 28);
    state.setBackground(Color.WHITE);
    state.setFont (new Font("MS Sans Serif", Font.PLAIN, 15));
    state.setText(" 0");
    cp.add(state);
    Ausgabe.setBounds(160, 88, 55, 16);
    Ausgabe.setText("Ausgabe");
    Ausgabe.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
    cp.add(Ausgabe);
    aus.setBounds(232, 80, 41, 28);
    aus.setBackground(Color.WHITE);
    aus.setFont (new Font("MS Sans Serif", Font.PLAIN, 15));
    aus.setText("");
    cp.add(aus);
    ueberschrift.setBounds(8, 8, 334, 20);
    ueberschrift.setText("Simulation eines Flip-Flops als Automat");
    ueberschrift.setFont (new Font("MS Sans Serif", Font.BOLD, 15));
    cp.add(ueberschrift);
    bildeAutomat();
    eingabeZeichen();
    ausgabeZeichen();
    zustaende();
    // Ende Komponenten

  }
  // Anfang Ereignisprozeduren
  public void SActionPerformed(ActionEvent evt) {
    automat(0);
  }

  public void RActionPerformed(ActionEvent evt) {
    automat(1);
  }

  public void WActionPerformed(ActionEvent evt) {
    automat(2);
  }

  public void BActionPerformed(ActionEvent evt) {
    automat(3);
  }
  void eingabeZeichen (  ) { //Zuweisen der Eingabetexte
       E[0]= "S";
       E[1]= "R";
       E[2]= "W";
       E[3]= "B";
  }
  void  ausgabeZeichen(  ) { // Zuweisen der Ausgabetexte
        A[0]="0";
        A[1]="1";
  }
  void zustaende (  ) { // Zuweisen der Zustandstexte
       sS[0]="S0";
       sS[1]="S1";
  }
  void bildeAutomat (){// Erstellen des Automaten als Zustands- und Ausgabematrix
     u[0][0]=1; u[0][1]=0; u[0][2]=1; u[0][3]=0; g[0][0]=0; g[0][1]=0; g[0][2]=0; g[0][3]=0;
     u[1][0]=1; u[1][1]=0; u[1][2]=0; u[1][3]=1; g[1][0]=1; g[1][1]=1; g[1][2]=1; g[1][3]=1;
  }
  void automat(int e) {  // Der Automat
               a= g[s][e]; //Ausgabezeichen
               aus.setText(" "+A[a]);
               s= u[s][e]; //Folgezustand
               state.setText(" "+sS[s]);
  }

  // Ende Ereignisprozeduren

}

