/*
================================================
		JKFlip.java
================================================
*/
import java.applet.*;
import java.awt.*;

public class JKFlip extends Applet
{
	//creating new objects that are instances of classes
	version1 ver1=new version1(this);
	TruthT  table1=new TruthT(this);
	/*in the design process the here class is used to 
	know the position of the cursor

	here cursor=new here(this);*/
	private Button bJ= new Button("J");
	private Button bK= new Button("K");
	private Label lclock= new Label("CLK 5V");
	Label lJ=new Label("0");
	Label lK=new Label("0");
	Label lA=new Label("A");
	Label lB=new Label("B");
	Label Qn1=new Label("Qn+1=");
	Label noQn1=new Label("/Qn+1=");
	Label Q=new Label("");
	Label noQ=new Label("");
	Label text=new Label("");
	boolean clock=false;
	int dx, dy;
	
	
	public JKFlip()
	{
	}

	public String getAppletInfo()
	{
		return "Name: JKFlip\r\n" +
		       "Author: Ana Cristina Fernandez\r\n" +
		       "Created with Microsoft Visual J++ Version 1.1";
	}
	public void init()
	{
		//adding buttons and labels to the applet
    	add(bK);
		add(bJ);
		add(lJ);
		add(lK);
		add(lA);
		add(lB);
		add(lclock);
		add(Q);
		add(noQ);
		add(Qn1);
		add(noQn1);
		add(text);
	}

	public void paint(Graphics g)
	{
		Color cl=g.getColor();
		g.setColor(Color.blue);
		g.drawString("JK Flip-Flop", 20,20);
		g.setColor(cl);
		//These are the only two values we need to define
		dx=120;
		dy=50;
		
		//placing all the gates invoking the method of version1 class
		ver1.coordenades(dx,dy);
		ver1.paint(g);
		
		g.drawLine(dx-15,dy+15,dx+20,dy+15);
		g.drawLine(dx-15,dy+115,dx+20,dy+115);
		g.drawLine(dx,dy,dx,dy-15);
		g.drawLine(dx,dy+130,dx,dy+145);
		g.drawLine(dx+190,dy+30,dx+230,dy+30);
		g.drawLine(dx+190,dy+100,dx+230,dy+100);
		
		g.drawLine(dx,dy-15,dx+210,dy-15);
		g.drawLine(dx+210, dy-15, dx+210, dy+100);
		g.drawLine(dx,dy+145,dx+220,dy+145);
		g.drawLine(dx+220,dy+145,dx+220,dy+30);
		g.drawLine(dx-15,dy+65, dx, dy+65);

		g.drawLine(dx-40, dy+65, dx-30, dy+65);
		g.drawLine(dx-20, dy+65, dx-15, dy+65);
		g.drawLine(dx-30,dy+65, dx-30,dy+50);
		g.drawLine(dx-20, dy+50, dx-20, dy+65);


		//in the design process we use this method to know the position of the cursor
		//cursor.paint(g);

		//defining the size and position of the buttons and labels
		lJ.reshape(dx-50 , dy+5 , 30, 20);
		lK.reshape(dx-50 , dy+105 , 30, 20);
		lA.reshape(195 , 40 , 20, 20);
		lB.reshape(195 , 140 , 20, 20);
		bJ.reshape(dx-70 , dy+5 , 20, 20);
		bK.reshape(dx-70 , dy+105 , 20, 20);
        lclock.reshape(dx-110 , dy+50 , 60, 20);
		Q.reshape(400 , 70, 50,20);
		noQ.reshape(400 , 142,50,20);
		Qn1.move(350 , 70);
		noQn1.move(350 , 142);
		text.reshape(350,105,80,20);

		table1.coordenades(100,250);
		table1.q0="Qn No Change";
		table1.q1="0";
		table1.q2="1";
		table1.q3="/Qn Toggle";
		table1.paint(g);

		//drawing the clock in red
		g.setColor(Color.red);
		if (clock) {g.drawLine(dx-33,dy+50,dx-17, dy+50);}
		else {g.drawLine(dx-33,dy+47,dx-17, dy+47);}

		
	}

	
	/*in the design method change the message of the coordenades everytime the cursor is moved 
	public boolean mouseMove(Event evt, int x, int y)
	{
		//record the mouse location
		cursor.m_dimCursorLoc=new Dimension(x,y);
		repaint();
		//cursor.paint(g);
		return true;
	}

	public boolean mouseDrag(Event evt, int x, int y)
	{
		return true;
	}
	*/
	//Defining the events
	public boolean action(Event e, Object obj) {
        //changing the label of bJ and bK every time these buttons are pressed 
		if (e.target==bJ) {
			if (lJ.getText()=="0"){
				lJ.setText("1");}
			else {
				lJ.setText("0");}
			}
		if (e.target==bK) {
			if (lK.getText()=="0"){
				lK.setText("1");}
			else {
				lK.setText("0");}
			}

        return true;
    }

	public boolean mouseDown(Event evt, int x, int y)
	{
		//changing the output everytime the clock is pressed
		if ((x>(dx-33)) & (x<(dx-17)) & (y<(dy+60)) & (y>(dy+40)) )
		{
			clock=true;
			repaint();
			if ((lJ.getText()=="0")&(lK.getText()=="0"))
			{
				text.setText("No Change");
				
			}
			
			else if ((lJ.getText()=="0")&(lK.getText()=="1"))
			{
				text.setText("Reset");
				Q.setText("0");
				noQ.setText("1");
			}
			else if ((lJ.getText()=="1")&(lK.getText()=="0"))
			{
				text.setText("Set");
				Q.setText("1");
				noQ.setText("0");
			}
			else if ((lJ.getText()=="1")&(lK.getText()=="1"))
			{
				text.setText(Q.getText());
				Q.setText(noQ.getText());
				noQ.setText(text.getText());
				text.setText("Toggle");
			}
		}

		return true;
	}

	//initial position of the clock when the mouse is up again
	public boolean mouseUp(Event evt, int x, int y)
	{
		clock=false;
		repaint();
		return true;
	}




}

	


