import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ArrayTeacher implements ActionListener { //The main frame / panel private JPanel primary; private JFrame frame; private JFrame frame2; //Memebers for the main window private JPanel main; private JPanel intro; private JPanel p1; private JPanel p2; private JPanel p3; private JPanel p4; private JPanel p5; private JLabel p1L1; private JLabel p2L1; private JLabel p2L2; private JLabel p2L3; private JLabel p4L1; private JTextField p4T1; private JTextField p4T2; private JButton p4B1; private JButton p4B2; private JLabel p4L2; private JLabel p4L3; private JButton p5B1; private JButton p5B2; private JLabel p5L1; private JLabel p5L2; //Members for the intro panel private JPanel ip1; private JLabel iL1; private JLabel iL2; private JTextField iT1; private JButton iB1; private JLabel iL3; private JLabel iL4; private JLabel iL5; private JLabel iL6; private JLabel iL7; private JLabel iL8; private JButton iB2; private JTextField iT2; private JTextField iT3; //The layout magic component... GridBagConstraints i1 = new GridBagConstraints(); //State variables int type; int rows; int cols; int pos; int walki; int walkj; int walk; //The actual information stored by the GUI int[] oneD; int[][] twoD; //The JButton arrays JButton[] joneD; JButton[][] jtwoD; public void actionPerformed(ActionEvent e) { try{ //Code for the intro window boolean createNewWindow=false; if (e.getSource() == iB1 && type == 0) { if (iT1.getText().length() < 1) return; type = 1; cols= Integer.valueOf(iT1.getText()); System.out.println("Create 1D array of size " + cols); createNewWindow = true; } if (e.getSource() == iB2 && type == 0) { if (iT2.getText().length() < 1 || iT3.getText().length() < 1) return; type = 2; rows = Integer.valueOf(iT2.getText()); cols = Integer.valueOf(iT3.getText()); createNewWindow = true; System.out.println("Create 2D array with " + rows + " rows and " + cols + " columns"); } //Start walk button if (e.getSource() == p5B1) { if (walk == 1) { //Reset the button array if (type == 1) joneD[cols-1].setText("?"); if (type == 2) jtwoD[rows-1][cols-1].setText("?"); walk=0; p2L2.setText(""); p2L3.setText(""); p5B1.setText("Start Walk"); p5B2.setVisible(false); p5L2.setText("--"); return; } p5B1.setText("Stop Walk"); if (type == 1) p2L2.setText("for (int i=0; i<" + String.valueOf(cols) + "; i++)"); if (type == 2) { p2L2.setText("for (int i=0; i<" + String.valueOf(rows) + "; i++)"); p2L3.setText(" for(int j=0;j<" + String.valueOf(cols) + ";j++)");; } p5B2.setVisible(true); if (type ==1) { pos=0; joneD[0].setText("*****"); p5L2.setText(String.valueOf(pos)); p4T1.setText(String.valueOf(pos)); p4L3.setText(String.valueOf(oneD[pos])); } if (type == 2) { walki=0; walkj=0; jtwoD[0][0].setText("****"); p5L2.setText(String.valueOf(walki)+","+String.valueOf(walkj)); p4T1.setText(String.valueOf(walki)); p4T1.setText(String.valueOf(walkj)); p4L3.setText(String.valueOf(twoD[walki][walkj])); } walk=1; } //Next button if (e.getSource() == p5B2) { if (type == 1) { if (pos >= cols) return; //Reset the array of buttons if (pos+1 < cols) joneD[pos].setText("?"); pos = pos +1; if (pos < cols) { joneD[pos].setText("****"); p4T1.setText(String.valueOf(pos)); p5L2.setText(String.valueOf(pos)); } else return; } if (type == 2) { if (walki >= rows) return; if (walki+1 -1 && pos < cols) { int value=oneD[pos]; p4L3.setText(String.valueOf(value)); } else p4L3.setText("Out of range"); } else if (type == 2 && walki < rows && walkj < cols) { if (p4T1.getText().length() < 1) return; if (p4T2.getText().length() < 1) return; walki = Integer.valueOf(p4T1.getText()); walkj = Integer.valueOf(p4T2.getText()); if (walki > -1 && walkj >-1 && walki < rows && walkj < cols) { int value = twoD[walki][walkj]; p4L3.setText(String.valueOf(value)); } else p4L3.setText("Out of range"); } else p4L3.setText("--"); } //Set button if (e.getSource() == p4B2) { //Make sure there is data in the text field to set if (p4T1.getText().length() < 1) return; String value=JOptionPane.showInputDialog("Enter a value"); if (type == 1) { pos = Integer.valueOf(p4T1.getText()); if (pos < oneD.length) oneD[pos] = Integer.valueOf(value); else { p4L3.setText("Invalid array position"); return; } } if (type == 2) { walki = Integer.valueOf(p4T1.getText()); walkj = Integer.valueOf(p4T2.getText()); if (walki < rows && walkj < cols) twoD[walki][walkj] = Integer.valueOf(value); else { p4L3.setText("Invalid array position"); return; } } p4L3.setText(String.valueOf(value)); } //Only happens if we are creating the new window for array visualization if (!createNewWindow) return; if (type == 1) oneD = new int[cols]; if (type == 2) twoD = new int[rows][cols]; System.out.println("Test"); createMain(); createMiddle(); layoutTop(); layoutBottom(); //CardLayout cl = (CardLayout)(main.getLayout()); //cl.show(main, "main"); frame2 = new JFrame(); frame2.setTitle("Array Visualization Main Window"); frame2.getContentPane().add(primary); frame2.pack(); frame2.setSize(frame2.getWidth()+50,frame2.getHeight()+50); frame2.setVisible(true); } catch (Exception ex) { System.out.println(ex); } } public void add(int x, int y, JPanel dest, Component toAdd) { i1.fill = GridBagConstraints.HORIZONTAL; i1.gridx = x; i1.gridy = y; i1.weightx=.5; dest.add(toAdd,i1); } public ArrayTeacher() { type=0; walk=0; //Create all the members JFrame frame = new JFrame(); primary = new JPanel(); createIntro(); layoutIntro(); //Show to the world frame.getContentPane().add(intro); frame.pack(); frame.setVisible(true); } public void createMain() { p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); p4 = new JPanel(); p5 = new JPanel(); p1L1 = new JLabel(); if (type == 1) p1L1.setText("Array of length: " + String.valueOf(cols)); if (type == 2) p1L1.setText("Array with " + String.valueOf(rows) + " Rows and " + String.valueOf(cols) + " columns "); //Panel 2 components p2L1 = new JLabel(); p2L1.setText("Code:"); p2L2 = new JLabel(); p2L3 = new JLabel(); //Panel 4 components p4L1 = new JLabel(); if (type == 1) p4L1.setText("Position"); if (type == 2) p4L1.setText("Row / Column"); p4T1 = new JTextField(2); p4T2 = new JTextField(2); p4B1 = new JButton(); p4B1.setText("Show"); p4B1.addActionListener(this); p4B2 = new JButton(); p4B2.addActionListener(this); p4B2.setText("Set"); p4L2 = new JLabel(); p4L2.setText("Value"); p4L3 = new JLabel(); p4L3.setText("---"); p5B1 = new JButton(); p5B1.addActionListener(this); p5B1.setText("Start walk"); p5B2 = new JButton(); p5B2.setText("Next"); p5B2.addActionListener(this); p5B2.setVisible(false); p5L1 = new JLabel(); p5L2 = new JLabel(); p5L1.setText("Position: "); p5L2.setText("--"); } //Create the array of buttons public void createMiddle() { if (type == 1 ) { p3.setLayout(new GridLayout(2,cols+1)); joneD = new JButton[cols]; for(int i=0; i