/*
=========================================================
			K_map.java

  Minimisation with Karnaugh Map (2 variables)
  It invokes methods of Cir1, cirfour, cirtwo
  It is not fully functional
=========================================================
*/
import java.applet.*;
import java.awt.*;

public class K_map extends Applet
{

	Button b1= new Button("0");
	Button b2= new Button("0");
	Button b3= new Button("0");
	Button b4= new Button("0");
	Button bgo= new Button("Minimisation");
	boolean mini=false;
	boolean oneb1=false;
	boolean oneb2=false;
	boolean oneb3=false;
	boolean oneb4=false;
	boolean twob1b2=false;
	boolean twob2b4=false;
	boolean twob1b3=false;
	boolean twob3b4=false;
	boolean fourb=false;
	Cir1 cir1=new Cir1(this);
	cirtwo cir2=new cirtwo(this);
	cirfour cir4=new cirfour(this);





	public K_map()
	{
	}

	public String getAppletInfo()
	{
		return "Name: K_map\r\n" +
		       "Author: Ana Cristina Fernandez\r\n" +
		       "Created with Microsoft Visual J++ Version 1.1";
	}


	public void init()
	{
    	add(b1);
		add(b2);
		add(b3);
		add(b4);
		add(bgo);

	}

	public void destroy()
	{
	}

	public void paint(Graphics g)
	{
		
		b1.reshape(40,40,40,40);
		b2.reshape(80,40,40,40);
		b3.reshape(40,80,40,40);
		b4.reshape(80,80,40,40);
		bgo.reshape(40,150,80,30);
		g.drawLine(10,10,40,40);
		g.drawString("A",30,25);
		g.drawString("B",20,40);

		g.drawString("0",55,35);
		g.drawString("1",95,35);
		g.drawString("0",30,65);
		g.drawString("1",30,105);

		if (mini=true){
			//here other table with the results and circles
			
			g.drawLine(10+150,10,40+150,40);
			g.drawString("A",30+150,25);
			g.drawString("B",20+150,40);
			g.drawString("0",55+150,35);
			g.drawString("1",95+150,35);
			g.drawString("0",30+150,65);
			g.drawString("1",30+150,105);
			g.drawLine(40+150,40,40+150+80,40);
			g.drawLine(40+150,40+40,40+150+80,40+40);
			g.drawLine(40+150,40+ 80,40+150+80,40+80);
			g.drawLine(40+150,40,40+150,40+80);
			g.drawLine(40+150+40,40,40+150+40,40+80);
			g.drawLine(40+150+80,40,40+150+80,40+80);

			g.drawString(b1.getLabel(),55+153,65);
			g.drawString(b2.getLabel(),95+153,65);
			g.drawString(b3.getLabel(),55+153,65+40);
			g.drawString(b4.getLabel(),95+153,65+40);



			int kx=40+150;
			int ky=40;
			if (oneb1) {
				cir1.coordenates(kx,ky);
				cir1.paint(g);}
			if (oneb2) {
				cir1.coordenates(kx+40,ky);
				cir1.paint(g);}
			if (oneb3) {
				cir1.coordenates(kx,ky+40);
				cir1.paint(g);}
			if (oneb4) {
				cir1.coordenates(kx+40,ky+40);
				cir1.paint(g);}
			if (twob1b2) {
				cir2.coordenates(kx,ky,0);
				cir2.paint(g);}
			if (twob1b3) {
				cir2.coordenates(kx,ky,1);
				cir2.paint(g);}
			if (twob3b4) {
				cir2.coordenates(kx,ky+40,0);
				cir2.paint(g);}
			if (twob2b4) {
				cir2.coordenates(kx+40,ky,1);
				cir2.paint(g);}
			if (fourb) {
				cir4.coordenates(kx,ky);
				cir4.paint(g);}
			
		}

	}

	public void start()
	{
	}
	
	public void stop()
	{
	}
	public boolean action(Event e, Object obj) {
        
		if (e.target==b1) {
			if (b1.getLabel()=="0"){
				b1.setLabel("1");}
			else {
				b1.setLabel("0");}
			}
		else if (e.target==b2) {
			if (b2.getLabel()=="0"){
				b2.setLabel("1");}
			else {
				b2.setLabel("0");}
			}
		else if (e.target==b3) {
			if (b3.getLabel()=="0"){
				b3.setLabel("1");}
			else {
				b3.setLabel("0");}
			}
		else if (e.target==b4) {
			if (b4.getLabel()=="0"){
				b4.setLabel("1");}
			else {
				b4.setLabel("0");}
			}
		else if (e.target==bgo) {
			mini = true;
			if (b1.getLabel()=="1"){
				oneb1=true;}
			if (b2.getLabel()=="1"){
				oneb2=true;}
			if (b3.getLabel()=="1"){
				oneb3=true;}
			if (b4.getLabel()=="1"){
				oneb4=true;}

			if ((oneb1)&(oneb2)){
				twob1b2=true;
				oneb1=false;
				oneb2=false;}
			if ((oneb1)&(oneb3)){
				twob1b3=true;
				oneb1=false;
				oneb3=false;}
			if ((oneb3)&(oneb4)){
				twob3b4=true;
				oneb3=false;
				oneb4=false;}
			if ((oneb2)&(oneb4)){
				twob2b4=true;
				oneb2=false;
				oneb4=false;}
			if ((twob1b2)&(twob3b4)){
				fourb=true;
				twob1b2=false;
				twob3b4=false;}
			if ((twob1b3)&(twob2b4)){
				fourb=true;
				twob1b3=false;
				twob2b4=false;
			}
			repaint();
		}
        return true;
    }
}




