import javax.swing.*; // for JFrame, JPanel, JTextField, JButton, JSlider, etc.
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.*; // for FlowLayout, GridLayout, BorderLayout, etc.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@SuppressWarnings("serial")
public class LayoutExample2 extends JFrame {
	private static final int WIDTH = 450;
	private static final int HEIGHT = 300;

	JTextField[][] aFields;
	JTextField[][] bFields;
	JTextField[][] cFields;
	
	// ******************************************************
	public LayoutExample2() {
		setTitle("Layout Example");
		setSize(WIDTH, HEIGHT);
		setLocation(100, 100);
		setLayout(new BorderLayout(15,25));
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		createContents();//
		setVisible(true);
	} // end LayoutExample2 constructor

	// ******************************************************
	private void createContents() {
		JPanel northPanel = new JPanel(new FlowLayout());
		JPanel southPanel = new JPanel(new FlowLayout());
		JPanel eastPanel = new JPanel(new BorderLayout());
		JPanel westPanel = new JPanel(new BorderLayout());
		JPanel centerPanel = new JPanel(new BorderLayout());
		JPanel eastGridPanel = new JPanel(new GridLayout(3, 3));
		JPanel westGridPanel = new JPanel(new GridLayout(3, 3));
		JPanel centerGridPanel = new JPanel(new GridLayout(3, 3));

		add(northPanel, BorderLayout.NORTH);
		add(southPanel, BorderLayout.SOUTH);
		add(eastPanel, BorderLayout.EAST);
		add(westPanel, BorderLayout.WEST);
		add(centerPanel, BorderLayout.CENTER);
		
		eastPanel.add(eastGridPanel, BorderLayout.CENTER);
		westPanel.add(westGridPanel, BorderLayout.CENTER);
		centerPanel.add(centerGridPanel, BorderLayout.CENTER);

		JSlider aHorizontal = new JSlider(JSlider.HORIZONTAL, 1, 10, 3);
		JSlider aVertical = new JSlider(JSlider.VERTICAL, 1, 10, 3);
		JSlider bHorizontal = new JSlider(JSlider.HORIZONTAL, 1, 10, 3);
		JSlider bVertical = new JSlider(JSlider.VERTICAL, 1, 10, 3);		
		JSlider cHorizontal = new JSlider(JSlider.HORIZONTAL, 1, 10, 3);
		JSlider cVertical = new JSlider(JSlider.VERTICAL, 1, 10, 3);
		
		westPanel.add(aHorizontal, BorderLayout.NORTH);
		westPanel.add(aVertical, BorderLayout.WEST);
		centerPanel.add(bHorizontal, BorderLayout.NORTH);
		centerPanel.add(bVertical, BorderLayout.WEST);
		eastPanel.add(cHorizontal, BorderLayout.NORTH);
		eastPanel.add(cVertical, BorderLayout.WEST);
				
		aHorizontal.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				westGridPanel.removeAll();
				int rows = aVertical.getValue();
				int cols = aHorizontal.getValue();
				westGridPanel.setLayout(new GridLayout(rows, cols));
				aFields = new JTextField[rows][cols];
				for (int i = 0; i < rows; i++) {
					for (int j = 0; j < cols; j++) {
						aFields[i][j] = new JTextField("0",5);
						westGridPanel.add(aFields[i][j]);
					}
				}
				validate();
				pack();
			}
		});
		
		aVertical.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				westGridPanel.removeAll();
				int rows = aVertical.getValue();
				int cols = aHorizontal.getValue();
				westGridPanel.setLayout(new GridLayout(rows, cols));
				aFields = new JTextField[rows][cols];
				for (int i = 0; i < rows; i++) {
					for (int j = 0; j < cols; j++) {
						aFields[i][j] = new JTextField("0",5);
						westGridPanel.add(aFields[i][j]);
					}
				}
				validate();
				pack();
			}
		});
		
		bHorizontal.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				centerGridPanel.removeAll();
				int rows = bVertical.getValue();
				int cols = bHorizontal.getValue();
				centerGridPanel.setLayout(new GridLayout(rows, cols));
				bFields = new JTextField[rows][cols];
				for (int i = 0; i < rows; i++) {
					for (int j = 0; j < cols; j++) {
						bFields[i][j] = new JTextField("0",5);
						centerGridPanel.add(bFields[i][j]);
					}
				}
				validate();
				pack();
			}
		});
		
		bVertical.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				centerGridPanel.removeAll();
				int rows = bVertical.getValue();
				int cols = bHorizontal.getValue();
				centerGridPanel.setLayout(new GridLayout(rows, cols));
				bFields = new JTextField[rows][cols];
				for (int i = 0; i < rows; i++) {
					for (int j = 0; j < cols; j++) {
						bFields[i][j] = new JTextField("0",5);
						centerGridPanel.add(bFields[i][j]);
					}
				}
				validate();
				pack();
			}
		});

		aFields = new JTextField[3][3];
		bFields = new JTextField[3][3];
		cFields = new JTextField[3][3];
		
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				aFields[i][j] = new JTextField("0",5);
				westGridPanel.add(aFields[i][j]);
			}
		}
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				bFields[i][j] = new JTextField("0",5);
				centerGridPanel.add(bFields[i][j]);
			}
		}
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				cFields[i][j] = new JTextField("0",5);
				eastGridPanel.add(cFields[i][j]);
			}
		}

		JButton addButton = new JButton("A+B");
		
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				eastGridPanel.removeAll();
				int rows = aVertical.getValue();
				int cols = aHorizontal.getValue();
				eastGridPanel.setLayout(new GridLayout(rows, cols));
				cFields = new JTextField[rows][cols];
				for (int i = 0; i < rows; i++) {
					for (int j = 0; j < cols; j++) {
						cFields[i][j] = new JTextField("0",5);
						eastGridPanel.add(cFields[i][j]);
					}
				}
				validate();
				pack();
				Matrix A = new Matrix(rows, cols);
				Matrix B = new Matrix(rows, cols);
				for (int i = 0; i < rows; i++) {
					for (int j = 0; j < 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 < rows; i++) {
					for (int j = 0; j < 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.
		
		JButton multiplyMButton = new JButton("AB");
		
		multiplyMButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				eastGridPanel.removeAll();
				
				int cRows = aVertical.getValue();
				int cCols = bHorizontal.getValue();
				
				eastGridPanel.setLayout(new GridLayout(cRows, cCols));
				cFields = new JTextField[cRows][cCols];
				for (int i = 0; i < cRows; i++) {
					for (int j = 0; j < cCols; j++) {
						cFields[i][j] = new JTextField("0",5);
						eastGridPanel.add(cFields[i][j]);
					}
				}
				validate();
				pack();
				
				int aRows = aVertical.getValue();
				int aCols = aHorizontal.getValue();
				
				int bRows = bVertical.getValue();
				int bCols = bHorizontal.getValue();
				
				Matrix A = new Matrix(aRows, aCols);
				Matrix B = new Matrix(bRows, bCols);
				
				for (int i = 0; i < aRows; i++) {
					for (int j = 0; j < aCols; j++) {
						A.M[i][j] = Double.parseDouble(aFields[i][j].getText());
					}
				}
				for (int i = 0; i < bRows; i++) {
					for (int j = 0; j < bCols; j++) {
						B.M[i][j] = Double.parseDouble(bFields[i][j].getText());
					}
				}
				Matrix C = A.multiplyM(B);
				for (int i = 0; i < cRows; i++) {
					for (int j = 0; j < cCols; j++) {
						cFields[i][j].setText("" + C.M[i][j]);
					}
				}
			}
		});
		
		southPanel.add(multiplyMButton);
		
		pack();

	} // end createContents

	// ******************************************************
	public static void main(String[] args) {
		new LayoutExample2();
	} // end main
} // end class LayoutExample