/** @author Scott Marshall @author University of Glasgow, MSc IT Project 2001 @author Building an OnLine Course in Computing Fundamentals */ package ukacgla_KMap; import java.util.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.table.*; import javax.swing.table.TableColumn; import javax.swing.table.TableCellRenderer; import javax.swing.JColorChooser; import javax.swing.table.AbstractTableModel; import javax.swing.SwingUtilities; import java.awt.*; import java.awt.event.*; import java.applet.Applet; /** Frame holds the GUI components of the exercise. */ abstract public class KMap_Frame extends JFrame { protected checkBoxListener listener; /** Number of options to be presented to user. */ protected int noOfOptions; protected JPanel kMap, buttonPanel, checkBoxPanel, mid1, mid2, labelPanel, host; protected Border border; protected JCheckBox[] checkBox; protected JButton clear,reveal,nextMap; protected JTextArea output; protected JLabel lab1; protected JTable table; protected Container contentPane; protected int noOfRows; //number of rows in myTableModel protected int noOfCols; //number of columns in myTableModel /** Sets titles of columns. */ protected String[] columnNames; /** Model of the TableModel. */ protected MyTableModel model; /** 2D Object array to hold data in TableModel. */ protected Object[][] data; /** Vector holds all Minterm objects for display. */ protected Vector answers; public KMap_Frame(JApplet m, int noOfRows, int noOfCols, String[] c){ contentPane = m.getContentPane(); columnNames = c; mkUI(); this.noOfRows = noOfRows; this.noOfCols = noOfCols; } /** Builds a new table model, calculates the answers, and redraws the panel on the screen */ protected abstract void newTable(); /** Builds the table to display the K Map, and the model to hold the data. After initial instantiation, the newTable() method should be called. */ protected abstract JPanel mkTable(); /** Builds the correct answers for the K Map */ protected abstract void buildAnswers(); /** Returns an panel containing the JCheckBox buttons. The checkBox array is set using the contents of the answers vector */ protected abstract JPanel mkButtonPanel(); /** Builds GUI and adds to applet content pane. Sets Look and Feel to Windows L&F. */ public void mkUI() { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception e) { } contentPane.setLayout(new BorderLayout(0,0)); //-----------K MAP --------------------- mid1 = new JPanel(new BorderLayout(10,10)); mid1.setBorder(new BevelBorder(BevelBorder.LOWERED)); JLabel lab1 = mkLabel("KMap for simplification ", Font.PLAIN, 10); mid1.add("North", lab1); //mkTable will also calculate the answer options and store these in a vector. kMap = mkTable(); mid1.add("Center", kMap); contentPane.add("North", mid1); //-----------CHECK BOXES --------------------- /** Building the check box array requires the correct answers. This method is called repeatedly, every time another k map is drawn. As a result it is self contained within its own method */ mkCheckBoxPanel(); //--------------- LABEL AND TXT AREA ------------------------------ JLabel lab3 = mkLabel("Program Output: ", Font.PLAIN, 10); output = mkTextArea(5,40,false); JPanel mid3 = new JPanel(new BorderLayout(10,10)); mid3.add("North", lab3); mid3.add("Center", output); //--------------- BUTTON PANEL ------------------------------ //make panel, make JButtons and add,set constraints and add add to frame buttonPanel = new JPanel( new FlowLayout() ); buttonPanel.setBorder(new BevelBorder(BevelBorder.LOWERED)); clear = mkButton("Reset", buttonPanel); clear.setToolTipText("Reset current question"); reveal = mkButton("Reveal", buttonPanel); reveal.setToolTipText("Displays correct option"); nextMap = mkButton("Next K Map", buttonPanel); nextMap.setToolTipText("Generates another random K Map"); mid3.add("South", buttonPanel); contentPane.add("South", mid3); } /** Adds the panel with the JCheckboxes to the contentPane. Called repeatedly when a new K Map is required. */ protected void mkCheckBoxPanel(){ if (mid2!=null) contentPane.remove(mid2); mid2 = new JPanel(new BorderLayout(10,10)); mid2.setBorder(new BevelBorder(BevelBorder.LOWERED)); checkBoxPanel = mkButtonPanel(); JLabel lab2 = mkLabel("Select one of following options :",Font.PLAIN, 10); mid2.add("North", lab2); mid2.add("Center", checkBoxPanel); contentPane.add("Center",mid2); contentPane.validate(); } /** Check boxes are given labels and action commands based on the Minterm object. */ protected JCheckBox mkChkButton(Container b,String name,String correct, boolean checked){ JCheckBox temp = new JCheckBox(name,checked); temp.setActionCommand(correct); temp.setPreferredSize(new Dimension(250,20)); temp.addItemListener(new checkBoxListener() ); b.add(temp); return temp; } /** Sets all boxes in GUI to de selected. Used for each new table. */ protected void clearChkbox() { for (int i = 0; i