|
TruthTableApp
TruthTableApp.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
public class TruthTableApp extends JApplet {
boolean isStandalone = false;
//JPanel InputPanel = new JPanel(new GridLayout(2,1,0,0)); // Connectives.NORTH
Box InputPanel = new Box(BoxLayout.Y_AXIS);
String []defaultChoices={"",
"I want to enter my own",
"("+Connectives.NEGATION+"A)",
"(A"+Connectives.AND+"B)",
"(A"+Connectives.OR+"B)",
"(A"+Connectives.XOR+"B)",
"(("+Connectives.NEGATION+"A)"+Connectives.XOR+"B)",
"(A"+Connectives.CONDITIONAL+"B)",
"(A"+Connectives.BICONDITIONAL+"B)",
"((A"+Connectives.OR+"B)"+Connectives.XOR+"(C"+Connectives.OR+"D))",
"(((A"+Connectives.OR+"B)"+Connectives.XOR+"(C"+Connectives.OR+"D))"+Connectives.AND+"E)" ,
"((((A"+Connectives.OR+"B)"+Connectives.XOR+"(C"+Connectives.OR+"D))"+Connectives.AND+"E)"
+Connectives.CONDITIONAL+
"(((F"+Connectives.OR+"G)"+Connectives.XOR+"(H"+Connectives.OR+"I))"+Connectives.AND+"J))",
};
JPanel IP1=new JPanel(new FlowLayout());
JComboBox ComboBoxInput = new JComboBox(defaultChoices); // for IP1
JLabel CBoxLabel=new JLabel("Choose One:");
JPanel ForCard=new JPanel(new FlowLayout());
CardLayout BLAH=new CardLayout(2,2);
JPanel CardPanel=new JPanel(BLAH);
JPanel IP2 = new JPanel(new GridLayout(2,1,1,1)); // For CardLayout
JTextField UserIn=new JTextField(); // for IP2
JPanel SymbolPanel = new JPanel(new GridLayout(1,12,5,5)); //for IP2
JPanel IP3 = new JPanel(); //For Card Layout
JPanel CenterPanel = new JPanel(); // CENTER
JPanel Bottom=new JPanel(new FlowLayout());
JTextArea Message=new JTextArea("",2,36);
TruthTable theTable= new TruthTable(); // for CENTER
JButton MeTry = new JButton(); // for NORTH
JButton GiveAnswer= new JButton(); // for NORTH
JButton CheckAnswer = new JButton(); // for NORTH
JButton AndButton= new JButton(); // for WEST
JButton OrButton= new JButton(); // for WEST
JButton XorButton= new JButton(); // for WEST
JButton NegationButton= new JButton(); // for WEST
JButton ConditionalButton= new JButton(); // for WEST
JButton BiconditionalButton= new JButton(); // for WEST
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
System.out.println("init");
this.setSize(new Dimension(450,500));
ComboBoxInput.setBackground(Color.white);
ComboBoxInput.setEditable(false);
ComboBoxInput.addActionListener(PropListen);
UserIn.addActionListener(UserInListen);
IP1.add(CBoxLabel);
IP1.add(ComboBoxInput,null);
InputPanel.add(IP1, null);
MeTry.setText("Let Me Try");
MeTry.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
MeTry_actionPerformed(e); } });
MeTry.setEnabled(false);
CheckAnswer.setText("Check Answer");
CheckAnswer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
CheckAnswer_actionPerformed(e); } });
CheckAnswer.setEnabled(false);
GiveAnswer.setText("Give Answer");
GiveAnswer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
GiveAnswer_actionPerformed(e); } });
GiveAnswer.setEnabled(false);
AndButton.setText(""+Connectives.AND);
OrButton.setText(""+Connectives.OR);
XorButton.setText(""+Connectives.XOR);
NegationButton.setText(""+Connectives.NEGATION);
ConditionalButton.setText(""+Connectives.CONDITIONAL);
BiconditionalButton.setText(""+Connectives.BICONDITIONAL);
SymbolPanel.add(AndButton,null);
SymbolPanel.add(OrButton,null);
SymbolPanel.add(XorButton,null);
SymbolPanel.add(NegationButton,null);
SymbolPanel.add(ConditionalButton,null);
SymbolPanel.add(BiconditionalButton,null);
IP2.add(UserIn,null);
IP2.add(SymbolPanel,null);
IP3.add(MeTry, null);
IP3.add(CheckAnswer, null);
IP3.add(GiveAnswer, null);
CardPanel.add(IP3,"ShowTable");
CardPanel.add(IP2,"UserInput");
ForCard.add(CardPanel,null);
InputPanel.add(ForCard, null);
CenterPanel.add(theTable);
theTable.setVisible(false);
Bottom.add(Message,null);
this.getContentPane().add(InputPanel, BorderLayout.NORTH);
this.getContentPane().add(CenterPanel, BorderLayout.CENTER);
this.getContentPane().add(Bottom, BorderLayout.SOUTH);
AndButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
AddSymbol(Connectives.AND);
}});
OrButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("OR"+e.toString());
AddSymbol(Connectives.OR);
}});
XorButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
AddSymbol(Connectives.XOR);
}});
NegationButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
AddSymbol(Connectives.NEGATION);
}});
ConditionalButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
AddSymbol(Connectives.CONDITIONAL);
}});
BiconditionalButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
AddSymbol(Connectives.BICONDITIONAL);
}});
Message.setBorder(BorderFactory.createEtchedBorder());
Message.setEditable(false);
Message.setRows(2);
Message.setLineWrap(true);
Message.setWrapStyleWord(true);
Message.setText("Choose a proposition from the menu, or create your own.");
FontFixer.setUIFont("Lucida Sans");
SwingUtilities.updateComponentTreeUI(this);
}
void AddSymbol(char A) {
System.out.println("blah");
UserIn.replaceSelection(A+"");
UserIn.requestFocus();
}
ActionListener UserInListen=new ActionListener() {
public void actionPerformed(ActionEvent e)
{ UserInListen_actionPerformed(e); } };
void UserInListen_actionPerformed(ActionEvent e) {
String prop=Connectives.toUnicode(UserIn.getText());
UserIn.setText(prop);
Proposition test=new Proposition(prop);
Message.setText(prop);
if(test.getProp().equals(Connectives.toUnicode(prop))) {
Message.append(" is a valid proposition");
Message.setBackground(Color.white);
ComboBoxInput.addItem(UserIn.getText());
ComboBoxInput.setSelectedItem(UserIn.getText());
}
else {
Message.append(" is NOT a valid proposition");
Message.setBackground(Color.red);
}
}
ActionListener PropListen=new ActionListener() {
public void actionPerformed(ActionEvent e)
{ ComboBoxInput_actionPerformed(e); } };
void ComboBoxInput_actionPerformed(ActionEvent e) {
if(ComboBoxInput.getSelectedIndex()==1) {
BLAH.show(CardPanel,"UserInput");
UserIn.requestFocus();
Message.setText("Enter your proposition in the area above. "+
"Press the ENTER key when you are done.");
return;
}
BLAH.show(CardPanel,"ShowTable");
if(ComboBoxInput.getSelectedIndex()==0) {
MeTry.setEnabled(false);
CheckAnswer.setEnabled(false);
GiveAnswer.setEnabled(false);
return;
}
String prop=(String) ComboBoxInput.getSelectedItem();
MeTry.setEnabled(true);
CheckAnswer.setEnabled(false);
GiveAnswer.setEnabled(true);
Message.setText("Click 'Let Me Try' to try it yourself,\n"+
"or 'Give Answer' to see the answer.");
theTable.setVisible(false);
CenterPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),"",
TitledBorder.CENTER, TitledBorder.ABOVE_TOP));
}
void MeTry_actionPerformed(ActionEvent e) {
String prop=(String) ComboBoxInput.getSelectedItem();
CenterPanel.remove(theTable);
theTable=new TruthTable();
theTable.initTable(prop);
CenterPanel.add(theTable);
theTable.setVisible(true);
int width=(int) Math.min(theTable.getPreferredSize().getWidth(),
CenterPanel.getWidth());
int height=(int) Math.min(theTable.getPreferredSize().getHeight(),
CenterPanel.getHeight()-45);
theTable.setPreferredSize(new Dimension(width,height));
Message.setText("Enter your guesses in the last column. "+
"Click 'Check Answer' to see how you did.");
CenterPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),
"\u03A3 = "+theTable.getProp()+" (Guess)",
TitledBorder.CENTER, TitledBorder.ABOVE_TOP,
new Font("Lucida Sans",Font.PLAIN,16),Color.blue));
CenterPanel.validate();
CheckAnswer.setEnabled(true);
MeTry.setEnabled(false);
GiveAnswer.setEnabled(false);
}
void CheckAnswer_actionPerformed(ActionEvent e) {
int numCorrect=theTable.gradeAnswer();
Message.setText("You got "+numCorrect+" out of "+
theTable.tTable.getRowCount() +" correct answers."+
" The correct answers are given above."+
" You got the green ones correct.\n"+
"Choose another one, or quit.");
CenterPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),
"\u03A3 = "+theTable.getProp(),
TitledBorder.CENTER, TitledBorder.ABOVE_TOP,
new Font("Lucida Sans",Font.PLAIN,16),Color.blue));
CheckAnswer.setEnabled(false);
}
void GiveAnswer_actionPerformed(ActionEvent e) {
String prop=(String) ComboBoxInput.getSelectedItem();
CenterPanel.remove(theTable);
theTable=new TruthTable();
theTable.initTable(prop);
CenterPanel.add(theTable);
theTable.setVisible(true);
int width=(int) Math.min(theTable.getPreferredSize().getWidth(),
CenterPanel.getWidth());
int height=(int) Math.min(theTable.getPreferredSize().getHeight(),
CenterPanel.getHeight()-40);
theTable.setPreferredSize(new Dimension(width,height));
theTable.setSize(new Dimension(width,height));
Message.setText("The Truth Table is given above.\n"+
"Choose another one, or quit.");
CenterPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),
"\u03A3 = "+theTable.getProp(),
TitledBorder.CENTER, TitledBorder.ABOVE_TOP,
new Font("Lucida Sans",Font.PLAIN,16),Color.blue));
CenterPanel.validate();
theTable.giveAnswer();
CheckAnswer.setEnabled(false);
MeTry.setEnabled(false);
GiveAnswer.setEnabled(false);
}
}
|