import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* @author Jenielle Gabriel
*
*/
public class FFGen extends JFrame{
private String FlipflopType = "";
private boolean isOneFlipflop = true;
private boolean isInput = false;
private boolean isOutput = false;
String JAEquation = "";
String KAEquation = "";
String JBEquation = "";
String KBEquation = "";
String OutputEquation = "";
public void First(){
/**
* @param args
*/
JLabel pleaseChooseFFType;
String[] FlipflopTypes = {"JK", "RS", "D", "T"};;
JComboBox FlipflopList;
JLabel pleaseChooseFFNum;
ButtonGroup FlipflopNumberGroup;
JRadioButtonMenuItem oneFlipflop;
JRadioButtonMenuItem twoFlipflop;
JLabel pleaseCheck;
JCheckBox oneInput;
JCheckBox oneOutput;
JButton next;
JFrame first;
JPanel firstConLeft;
JPanel firstConRight;
FlipflopList = new JComboBox(FlipflopTypes);
FlipflopList.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
JComboBox cb = (JComboBox)e.getSource();
FlipflopType = (String)cb.getSelectedItem();
}
}
);
pleaseChooseFFType = new JLabel("Type of Flipflop:");
oneFlipflop = new JRadioButtonMenuItem ("1");
oneFlipflop.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
isOneFlipflop = true;
}
}
);
twoFlipflop = new JRadioButtonMenuItem ("2");
twoFlipflop.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
isOneFlipflop = false;
}
}
);
pleaseChooseFFNum = new JLabel("Number of Flipflops:");
FlipflopNumberGroup = new ButtonGroup();
FlipflopNumberGroup.add(oneFlipflop);
FlipflopNumberGroup.add(twoFlipflop);
pleaseCheck = new JLabel("There is an:");
oneInput = new JCheckBox ("Input");
oneInput.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED)
isInput = true;
else if (e.getStateChange() == ItemEvent.DESELECTED)
isInput = false;
}
}
);
oneOutput = new JCheckBox ("Output");
oneOutput.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED)
isOutput = true;
else if (e.getStateChange() == ItemEvent.DESELECTED)
isOutput = false;
}
}
);
next = new JButton ("Next");
next.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if(FlipflopType == "JK")
JK();
}
}
);
first = new JFrame();
firstConLeft = new JPanel();
firstConRight = new JPanel();
firstConRight.setLayout(new GridLayout(9,1,0,0));
firstConRight.add(pleaseChooseFFType);
firstConRight.add(FlipflopList);
firstConRight.add(pleaseChooseFFNum);
firstConRight.add(oneFlipflop);
firstConRight.add(twoFlipflop);
firstConRight.add(pleaseCheck);
firstConRight.add(oneInput);
firstConRight.add(oneOutput);
firstConRight.add(next);
JPanel Empty = new JPanel();
Empty.setLayout(new GridLayout(1,2));
Empty.add(firstConLeft);
Empty.add(firstConRight);
first.add(Empty);
first.setSize(250,235);
first.setVisible(true);
//first.validate();
}
public void JK(){
JFrame JKFrame = new JFrame();
JButton A = new JButton("A");
JButton B = new JButton("B");
JButton x = new JButton("x");
JButton open = new JButton ("(");
JButton close = new JButton (")");
JButton and = new JButton("*");
JButton or = new JButton("+");
final JTextField JAEq = new JTextField();
final JTextField KAEq = new JTextField();
final JTextField JBEq = new JTextField();
final JTextField KBEq = new JTextField();
final JTextField OutputEq = new JTextField();
JPanel JKLeftPanel = new JPanel();
JPanel JKRightPanel = new JPanel();
JKRightPanel.setLayout(new GridLayout(7,2));
JKRightPanel.add(new JLabel("Enter the equations. "));
JKRightPanel.add(new JLabel("Observe Proper Syntax."));
JKRightPanel.add(new JLabel("JA = "));
JKRightPanel.add(JAEq);
JKRightPanel.add(new JLabel("JB = "));
JKRightPanel.add(JBEq);
JKRightPanel.add(new JLabel("KA = "));
JKRightPanel.add(KAEq);
JKRightPanel.add(new JLabel("KB = "));
JKRightPanel.add(KBEq);
JKRightPanel.add(new JLabel("x = "));
JKRightPanel.add(OutputEq);
JKRightPanel.add(new JLabel("Generate: "));
JButton next = new JButton("next");
JKRightPanel.add(next);
next.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JAEquation = JAEq.getText();
JBEquation = JAEq.getText();
KAEquation = KAEq.getText();
KBEquation = KAEq.getText();
OutputEquation = OutputEq.getText();
genarateJK();
}
}
);
JKFrame.add(JKRightPanel);
JKFrame.setSize(250,235);
JKFrame.setVisible(true);
}
public void genarateJK(){
JFrame JKTableFrame = new JFrame();
int c = 0;
int nextA[] = {0,0}; //Storage for next state A (has default values)
int nextB[]= {0,0}; //Storage for next state B (has default values)
int JA[] = {0,0}; //Storage for JA Values
int JB[] = {0,0}; //Storage for JB Values
int KA[] = {0,0}; //Storage for KA Values
int KB[] = {0,0}; //Storage for KB Values
if(isOneFlipflop && !isInput){ // one flipflop, no input, no output
String[] A = {"1","0"};
c = 2;
for(int j=0; j<c; j++){
//Complement of A
String AComp = "";
if(A[j] == "0")
AComp = "1";
else
AComp = "0";
//replace JA Formula with current value of A'
String subsJA = replace(JAEquation,"A'",AComp);
while(!subsJA.equals(replace(JAEquation,"A'",AComp))){
subsJA = replace(JAEquation,"A",A[j]);
}
//replace JA Formula with current value of A
subsJA = replace(JAEquation,"A",A[j]);
while(!subsJA.equals(replace(JAEquation,"A",A[j]))){
subsJA = replace(JAEquation,"A",A[j]);
}
//replace KA Formula with current value of A'
String subsKA = replace(KAEquation,"A'",AComp);
while(!subsKA.equals(replace(KAEquation,"A",AComp))){
subsKA = replace(KAEquation,"A",A[j]);
}
//replace KA Formula with current value of A
subsKA = replace(KAEquation,"A",A[j]);
while(!subsKA.equals(replace(KAEquation,"A",A[j]))){
subsKA = replace(KAEquation,"A",A[j]);
}
try{
Parser parser = new Parser();
//compute the value of JA
int computedJAValue = (int)parser.evaluate(subsJA);
JA[j] = computedJAValue;
//compute the value of KA
int computedKAValue = (int)parser.evaluate(subsKA);
KA[j] = computedKAValue;
}catch(Exception e){
//testlang.setText(e.getMessage());
}
if(JA[j] == 0 && KA[j] == 0){
nextA[j] = Integer.parseInt(A[j]);
}
else if(JA[j] == 0 && KA[j] == 1){
nextA[j] = 0;
}
else if(JA[j] == 1 && KA[j] == 0){
nextA[j] = 1;
}
else if(JA[j] == 1 && KA[j] == 1){
if(A[j] == "0")
nextA[j] = 1;
else if(A[j] == "1")
nextA[j] = 0;
}
}//End for loop
JKTableFrame.setLayout(new GridLayout(1,4));
JKTableFrame.add(new JLabel("<html>Present<br>States<br>"+A[0]+"<br>"+A[1]));
JKTableFrame.add(new JLabel("<html>JA<br><br>"+JA[0]+"<br>"+JA[1]));
JKTableFrame.add(new JLabel(JAEquation));
//JKTableFrame.add(new JLabel("<html>KA<br><br>"+KA[0]+"<br>"+KA[1]));
JKTableFrame.add(new JLabel("<html>Next<br>States<br>"+nextA[0]+"<br>"+nextA[1]));
}//End one flipflop, no input, no output
JKTableFrame.setSize(250,235);
JKTableFrame.setVisible(true);
}
static String replace(String str, String pattern, String replace) {
int s = 0;
int e = 0;
StringBuffer result = new StringBuffer();
while ((e = str.indexOf(pattern, s)) >= 0) {
result.append(str.substring(s, e));
result.append(replace);
s = e+pattern.length();
}
result.append(str.substring(s));
return result.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FFGen gui = new FFGen();
gui.First();
gui.repaint();
}
}