import javax.swing.*; // for JFrame, JPanel, JTextField, JButton, JOptionPane, etc.
import java.awt.*; // for FlowLayout, GridLayout, BorderLayout, etc.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@SuppressWarnings("serial")
public class LayoutExample extends JFrame {
	private static final int WIDTH = 450;
	private static final int HEIGHT = 300;

	JTextField[][] aFields;
	JTextField[][] bFields;
	JTextField[][] cFields;
	
	int a_rows, a_cols, b_rows, b_cols;

	// ******************************************************
	public LayoutExample(int a_rows, int a_cols, int b_rows, int b_cols) {
		this.a_rows = a_rows;
		this.a_cols = a_cols;
		this.b_rows = b_rows;
		this.b_cols = b_cols;
		
		setTitle("Layout Example");
		setSize(WIDTH, HEIGHT);
		setLayout(new BorderLayout(15,25));
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		createContents();
		setLocationRelativeTo(null); // after adding all the components, center the JFrame 
		setVisible(true);
	} // end LayoutExample constructor

	// ******************************************************
	private void createContents() {
		JPanel northPanel = new JPanel(new FlowLayout());
		JPanel southPanel = new JPanel(new FlowLayout());
		
		// If A and B are correctly dimensioned for either addition or matrix multiplication, 
		// this will ensure C has the correct dimensions to hold A+B or AB
		JPanel eastPanel = new JPanel(new GridLayout(a_rows, b_cols));
		
		JPanel westPanel = new JPanel(new GridLayout(a_rows, a_cols));
		JPanel centerPanel = new JPanel(new GridLayout(b_rows, b_cols));

		add(northPanel, BorderLayout.NORTH);
		add(southPanel, BorderLayout.SOUTH);
		add(eastPanel, BorderLayout.EAST);
		add(westPanel, BorderLayout.WEST);
		add(centerPanel, BorderLayout.CENTER);

		aFields = new JTextField[a_rows][a_cols];
		bFields = new JTextField[b_rows][b_cols];
		cFields = new JTextField[a_rows][b_cols];
		
		for (int i = 0; i < a_rows; i++) {
			for (int j = 0; j < a_cols; j++) {
				aFields[i][j] = new JTextField("0",5);
				westPanel.add(aFields[i][j]);
			}
		}
		for (int i = 0; i < b_rows; i++) {
			for (int j = 0; j < b_cols; j++) {
				bFields[i][j] = new JTextField("0",5);
				centerPanel.add(bFields[i][j]);
			}
		}
		for (int i = 0; i < a_rows; i++) {
			for (int j = 0; j < b_cols; j++) {
				cFields[i][j] = new JTextField("0",5);
				eastPanel.add(cFields[i][j]);
			}
		}

		JButton addButton = new JButton("A+B");
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (a_rows != b_rows || a_cols != b_cols) {
					JOptionPane.showMessageDialog(addButton, "incorrect dimensions", "error", JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				Matrix A = new Matrix(a_rows, a_cols);
				Matrix B = new Matrix(a_rows, a_cols);
				for (int i = 0; i < a_rows; i++) {
					for (int j = 0; j < a_cols; j++) {
						A.M[i][j] = Double.parseDouble(aFields[i][j].getText());
						B.M[i][j] = Double.parseDouble(bFields[i][j].getText());
					}
				}	
				Matrix C = A.add(B);
				for (int i = 0; i < a_rows; i++) {
					for (int j = 0; j < a_cols; j++) {
						cFields[i][j].setText("" + C.M[i][j]);
					}
				}
			}
		});
		southPanel.add(addButton);
		
		// Add a matrix product button (AB) here.  Use the A+B button as an example.
		


		pack();

	} // end createContents

	// ******************************************************
	public static void main(String[] args) {
		int a_rows = Integer.parseInt(JOptionPane.showInputDialog("a_rows: "));
		int a_cols = Integer.parseInt(JOptionPane.showInputDialog("a_cols: "));
		int b_rows = Integer.parseInt(JOptionPane.showInputDialog("b_rows: "));
		int b_cols = Integer.parseInt(JOptionPane.showInputDialog("b_cols: "));
		new LayoutExample(a_rows, a_cols, b_rows, b_cols);
	} // end main
} // end class LayoutExample