
import java.awt.*;
import java.applet.*;
import java.awt.event.*;


public class CG_hw_03 extends Applet implements ActionListener{

	TextField t1, t2, t3, t4, t5,t6,t7,t8,t9;
	TextField t11, t21, t31, t41, t51, t61, t71, t81, t91;
	TextField t111, t211, t311, t411, t511, t611, t711, t811, t911;
	
	Button x00inc, x01inc, x02inc, x10inc, x11inc, x12inc, x20inc, x21inc, x22inc;
	Button x00dec, x01dec, x02dec, x10dec, x11dec, x12dec, x20dec, x21dec, x22dec;
	
	Button y00inc, y01inc, y02inc, y10inc, y11inc, y12inc, y20inc, y21inc, y22inc;
	Button y00dec, y01dec, y02dec, y10dec, y11dec, y12dec, y20dec, y21dec, y22dec;
	
	Button addButton, multButton;
	String lastAction;
	
	
	Label matA = new Label("Matrix A");
	
	
	public void createButton(Button a, String shape, int sizeWidth, int sizeHeight, int locX, int locY)
	{
//		a = new Button(shape);
		a.setSize(sizeWidth, sizeHeight);
		a.setLocation(locX,locY);
		add(a);
		a.addActionListener(this);
		
	}
	
	public void createTextField(TextField t, String initial, int length,int tWidth, int tHeight, int locX, int locY)
	{
//		t = new TextField(initial,length);
		t.setSize(tWidth, tHeight);
		t.setLocation(locX,locY);
		add(t);
	}
	
	
	public void init() {
		
		setLayout(null);
		int textWidth=40;
		int textLength=30;
	

		
		multButton = new Button("Multiply");
		multButton.setLocation(220,300);
		multButton.setSize(60,20);
		add(multButton);


		addButton = new Button("Add");
		addButton.setLocation(150,300);
		addButton.setSize(40,20);
		add(addButton);




		x00inc = new Button("^");
		x01inc = new Button("^");
		x02inc = new Button("^");
		x10inc = new Button("^");
		x11inc = new Button("^");
		x12inc = new Button("^");
		x20inc = new Button("^");
		x21inc = new Button("^");
		x22inc = new Button("^");
		
		
		y00inc = new Button("^");
		y01inc = new Button("^");
		y02inc = new Button("^");
		y10inc = new Button("^");
		y11inc = new Button("^");
		y12inc = new Button("^");
		y20inc = new Button("^");
		y21inc = new Button("^");
		y22inc = new Button("^");
		
		
		x00dec = new Button("v");
		x01dec = new Button("v");
		x02dec = new Button("v");
		x10dec = new Button("v");
		x11dec = new Button("v");
		x12dec = new Button("v");
		x20dec = new Button("v");
		x21dec = new Button("v");
		x22dec = new Button("v");
		
		
		y00dec = new Button("v");
		y01dec = new Button("v");
		y02dec = new Button("v");
		y10dec = new Button("v");
		y11dec = new Button("v");
		y12dec = new Button("v");
		y20dec = new Button("v");
		y21dec = new Button("v");
		y22dec = new Button("v");


		
		
		t1 = new TextField("1.0",1);
		t2 = new TextField("0.0",1);
		t3 = new TextField("0.0",1);
		t4 = new TextField("0.0",1);
		t5 = new TextField("1.0",1);
		t6 = new TextField("0.0",1);
		t7 = new TextField("0.0",1);
		t8 = new TextField("0.0",1);
		t9 = new TextField("1.0",3);
	
		
		
		
		t11 = new TextField("1.0",4);
		t21 = new TextField("0.0",4);
		t31 = new TextField("0.0",4);
		t41 = new TextField("0.0",4);
		t51 = new TextField("1.0",4);
		t61 = new TextField("0.0",4);
		t71 = new TextField("0.0",4);
		t81 = new TextField("0.0",4);
		t91 = new TextField("1.0",4);
		
		
		t111 = new TextField("1.0",3);
		t211 = new TextField("0.0",3);
		t311 = new TextField("0.0",3);
		t411 = new TextField("0.0",3);
		t511 = new TextField("1.0",3);
		t611 = new TextField("0.0",3);
		t711 = new TextField("0.0",3);
		t811 = new TextField("0.0",3);
		t911 = new TextField("1.0",3);
		
		
		
				
		

		createTextField(t1,"1",3,textWidth,textLength,10,10);
		createButton(x00inc,"^",20,15,50,10);
		createButton(x00dec,"v",20,15,50,25);
		
		createTextField(t2,"0",3,textWidth,textLength,70,10);
		createButton(x01inc,"^",20,15,110,10);
		createButton(x01dec,"v",20,15,110,25);
		
		createTextField(t3,"0",3,textWidth,textLength,130,10);
		createButton(x02inc,"^",20,15,170,10);
		createButton(x02dec,"v",20,15,170,25);
		
		
		
		createTextField(t4,"0",3,textWidth,textLength,10,40);
		createButton(x10inc,"^",20,15,50,40);
		createButton(x10dec,"v",20,15,50,55);
		
		createTextField(t5,"1",3,textWidth,textLength,70,40);
		createButton(x11inc,"^",20,15,110,40);
		createButton(x11dec,"v",20,15,110,55);
		
		createTextField(t6,"0",3,textWidth,textLength,130,40);
		createButton(x12inc,"^",20,15,170,40);
		createButton(x12dec,"v",20,15,170,55);	
		


		createTextField(t7,"0",3,textWidth,textLength,10,70);
		createButton(x20inc,"^",20,15,50,70);
		createButton(x20dec,"v",20,15,50,85);
		
		createTextField(t8,"0",3,textWidth,textLength,70,70);
		createButton(x21inc,"^",20,15,110,70);
		createButton(x21dec,"v",20,15,110,85);
		
		createTextField(t9,"1",3,textWidth,textLength,130,70);
		createButton(x22inc,"^",20,15,170,70);
		createButton(x22dec,"v",20,15,170,85);





		createTextField(t11,"1",3,textWidth,textLength,250,10);
		createButton(y00inc,"^",20,15,290,10);
		createButton(y00dec,"v",20,15,290,25);
		
		createTextField(t21,"0",3,textWidth,textLength,310,10);
		createButton(y01inc,"^",20,15,350,10);
		createButton(y01dec,"v",20,15,350,25);
		
		createTextField(t31,"0",3,textWidth,textLength,370,10);
		createButton(y02inc,"^",20,15,410,10);
		createButton(y02dec,"v",20,15,410,25);
		
		
				
		createTextField(t41,"0",3,textWidth,textLength,250,40);
		createButton(y10inc,"^",20,15,290,40);
		createButton(y10dec,"v",20,15,290,55);
		
		createTextField(t51,"1",3,textWidth,textLength,310,40);
		createButton(y11inc,"^",20,15,350,40);
		createButton(y11dec,"v",20,15,350,55);
		
		createTextField(t61,"0",3,textWidth,textLength,370,40);
		createButton(y12inc,"^",20,15,410,40);
		createButton(y12dec,"v",20,15,410,55);	
		
		
		
		createTextField(t71,"0",3,textWidth,textLength,250,70);
		createButton(y20inc,"^",20,15,290,70);
		createButton(y20dec,"v",20,15,290,85);
		
		createTextField(t81,"0",3,textWidth,textLength,310,70);
		createButton(y21inc,"^",20,15,350,70);
		createButton(y21dec,"v",20,15,350,85);
	
		createTextField(t91,"1",3,textWidth,textLength,370,70);
		createButton(y22inc,"^",20,15,410,70);
		createButton(y22dec,"v",20,15,410,85);
		
		
		

		
		createTextField(t111,"1",3,textWidth,textLength,150,150);
		createTextField(t211,"0",3,textWidth,textLength,190,150);
		createTextField(t311,"0",3,textWidth,textLength,230,150);
		
		createTextField(t411,"0",3,textWidth,textLength,150,180);
		createTextField(t511,"1",3,textWidth,textLength,190,180);
		createTextField(t611,"0",3,textWidth,textLength,230,180);
	
		createTextField(t711,"0",3,textWidth,textLength,150,210);
		createTextField(t811,"0",3,textWidth,textLength,190,210);
		createTextField(t911,"1",3,textWidth,textLength,230,210);


/*
		x00inc.addActionListener(this);
		x01inc.addActionListener(this);
		x02inc.addActionListener(this);
		x10inc.addActionListener(this);
		x11inc.addActionListener(this);
		x12inc.addActionListener(this);
		x20inc.addActionListener(this);
		x21inc.addActionListener(this);
		x22inc.addActionListener(this);
		
		
		y00inc.addActionListener(this);
		y01inc.addActionListener(this);
		y02inc.addActionListener(this);
		y10inc.addActionListener(this);
		y11inc.addActionListener(this);
		y12inc.addActionListener(this);
		y20inc.addActionListener(this);
		y21inc.addActionListener(this);
		y22inc.addActionListener(this);
		
		
		x00dec.addActionListener(this);
		x01dec.addActionListener(this);
		x02dec.addActionListener(this);
		x10dec.addActionListener(this);
		x11dec.addActionListener(this);
		x12dec.addActionListener(this);
		x20dec.addActionListener(this);
		x21dec.addActionListener(this);
		x22dec.addActionListener(this);
		
		
		y00dec.addActionListener(this);
		y01dec.addActionListener(this);
		y02dec.addActionListener(this);
		y10dec.addActionListener(this);
		y11dec.addActionListener(this);
		y12dec.addActionListener(this);
		y20dec.addActionListener(this);
		y21dec.addActionListener(this);
		y22dec.addActionListener(this);
		
*/		
		
		addButton.addActionListener(this);
		multButton.addActionListener(this);
		
		
		
			

	}

	public void paint(Graphics g) {
		
		g.drawString("Matrix A",70,120);
		g.drawString("Matrix B",310,120);
		g.drawString("Answer",190,260);

	}
	
	
	
	public void actionPerformed(ActionEvent e)
	{
		String label = (String)e.getActionCommand();
		
//		System.out.println(label);

		
		double x00 = Double.valueOf(t1.getText()).doubleValue();
		double x01 = Double.valueOf(t2.getText()).doubleValue();
		double x02 = Double.valueOf(t3.getText()).doubleValue();
		double x10 = Double.valueOf(t4.getText()).doubleValue();
		double x11 = Double.valueOf(t5.getText()).doubleValue();
		double x12 = Double.valueOf(t6.getText()).doubleValue();
		double x20 = Double.valueOf(t7.getText()).doubleValue();
		double x21 = Double.valueOf(t8.getText()).doubleValue();
		double x22 = Double.valueOf(t9.getText()).doubleValue();
		
		
		double y00 = Double.valueOf(t11.getText()).doubleValue();
		double y01 = Double.valueOf(t21.getText()).doubleValue();
		double y02 = Double.valueOf(t31.getText()).doubleValue();
		double y10 = Double.valueOf(t41.getText()).doubleValue();
		double y11 = Double.valueOf(t51.getText()).doubleValue();
		double y12 = Double.valueOf(t61.getText()).doubleValue();
		double y20 = Double.valueOf(t71.getText()).doubleValue();
		double y21 = Double.valueOf(t81.getText()).doubleValue();
		double y22 = Double.valueOf(t91.getText()).doubleValue();



		if(e.getSource().equals(x00inc))
		{
			x00++;
			t1.setText(""+x00);
		}
		
		else if(e.getSource().equals(x01inc))
		{
			x01++;
			t2.setText(""+x01);
		}

		else if(e.getSource().equals(x02inc))
		{
			x02++;
			t3.setText(""+x02);
		}
		
		else if(e.getSource().equals(x10inc))
		{
			x10++;
			t4.setText(""+x10);
		}
		
		else if(e.getSource().equals(x11inc))
		{
			x11++;
			t5.setText(""+x11);
		}
		
		else if(e.getSource().equals(x12inc))
		{
			x12++;
			t6.setText(""+x12);
		}

		else if(e.getSource().equals(x20inc))
		{
			x20++;
			t7.setText(""+x20);
		}
		
		
		else if(e.getSource().equals(x21inc))
		{
			x21++;
			t8.setText(""+x21);
		}	
		
		
		else if(e.getSource().equals(x22inc))
		{
			x22++;
			t9.setText(""+x22);
		}	



		if(e.getSource().equals(y00inc))
		{
			y00++;
			t11.setText(""+y00);
		}
		
		else if(e.getSource().equals(y01inc))
		{
			y01++;
			t21.setText(""+y01);
		}

		else if(e.getSource().equals(y02inc))
		{
			y02++;
			t31.setText(""+y02);
		}
		
		else if(e.getSource().equals(y10inc))
		{
			y10++;
			t41.setText(""+y10);
		}
		
		else if(e.getSource().equals(y11inc))
		{
			y11++;
			t51.setText(""+y11);
		}
		
		else if(e.getSource().equals(y12inc))
		{
			y12++;
			t61.setText(""+y12);
		}

		else if(e.getSource().equals(y20inc))
		{
			y20++;
			t71.setText(""+y20);
		}
		
		
		else if(e.getSource().equals(y21inc))
		{
			y21++;
			t81.setText(""+y21);
		}	
		
		
		else if(e.getSource().equals(y22inc))
		{
			y22++;
			t91.setText(""+y22);
		}
		
		



		if(e.getSource().equals(x00dec))
		{
			x00--;
			t1.setText(""+x00);
		}
		
		else if(e.getSource().equals(x01dec))
		{
			x01--;
			t2.setText(""+x01);
		}

		else if(e.getSource().equals(x02dec))
		{
			x02--;
			t3.setText(""+x02);
		}
		
		else if(e.getSource().equals(x10dec))
		{
			x10--;
			t4.setText(""+x10);
		}
		
		else if(e.getSource().equals(x11dec))
		{
			x11--;
			t5.setText(""+x11);
		}
		
		else if(e.getSource().equals(x12dec))
		{
			x12--;
			t6.setText(""+x12);
		}

		else if(e.getSource().equals(x20dec))
		{
			x20--;
			t7.setText(""+x20);
		}
		
		
		else if(e.getSource().equals(x21dec))
		{
			x21--;
			t8.setText(""+x21);
		}	
		
		
		else if(e.getSource().equals(x22dec))
		{
			x22--;
			t9.setText(""+x22);
		}	



		if(e.getSource().equals(y00dec))
		{
			y00--;
			t11.setText(""+y00);
		}
		
		else if(e.getSource().equals(y01dec))
		{
			y01--;
			t21.setText(""+y01);
		}

		else if(e.getSource().equals(y02dec))
		{
			y02--;
			t31.setText(""+y02);
		}
		
		else if(e.getSource().equals(y10dec))
		{
			y10--;
			t41.setText(""+y10);
		}
		
		else if(e.getSource().equals(y11dec))
		{
			y11--;
			t51.setText(""+y11);
		}
		
		else if(e.getSource().equals(y12dec))
		{
			y12--;
			t61.setText(""+y12);
		}

		else if(e.getSource().equals(y20dec))
		{
			y20--;
			t71.setText(""+y20);
		}
		
		
		else if(e.getSource().equals(y21dec))
		{
			y21--;
			t81.setText(""+y21);
		}	
		
		
		else if(e.getSource().equals(y22dec))
		{
			y22--;
			t91.setText(""+y22);
		}
		
		
		
				
		
		
		double m00 = (x00*y00)+(x01*y10)+(x02*y20);
		double m01 = (x00*y01)+(x01*y11)+(x02*y21);
		double m02 = (x00*y02)+(x01*y12)+(x02*y22);

		double m10 = (x10*y00)+(x11*y10)+(x12*y20);
		double m11 = (x10*y01)+(x11*y11)+(x12*y21);
		double m12 = (x10*y02)+(x11*y12)+(x12*y22);
		
		double m20 = (x20*y00)+(x21*y10)+(x22*y20);
		double m21 = (x20*y01)+(x21*y11)+(x22*y21);
		double m22 = (x20*y02)+(x21*y12)+(x22*y22);	
		
		
		
		double a00 = x00+y00;
		double a01 = x01+y01;
		double a02 = x02+y02;
		
		double a10 = x10+y10;
		double a11 = x11+y11;
		double a12 = x12+y12;
		
		double a20 = x20+y20;
		double a21 = x21+y21;
		double a22 = x22+y22;	

		
		if(label == "Multiply" || (lastAction == "Multiply" && label != "Add"))
		{
			lastAction = "Multiply";
			t111.setText(""+m00);
			t211.setText(""+m01);
			t311.setText(""+m02);
			
			t411.setText(""+m10);
			t511.setText(""+m11);
			t611.setText(""+m12);
			
			t711.setText(""+m20);
			t811.setText(""+m21);
			t911.setText(""+m22);
			
			
		}
		
		else if (label == "Add" || (lastAction == "Add" && label != "Multiply"))
		{
			lastAction = "Add";
			t111.setText(""+a00);
			t211.setText(""+a01);
			t311.setText(""+a02);
			
			t411.setText(""+a10);
			t511.setText(""+a11);
			t611.setText(""+a12);
			
			t711.setText(""+a20);
			t811.setText(""+a21);
			t911.setText(""+a22);
			
		}
		
		
	}


}

