/*
================================================
		RS3.java

  Clocked RS Flip-Flop
================================================
*/
import java.applet.*;
import java.awt.*;

public class RS3 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 bR= new Button("R");
	private Button bS= new Button("S");
	private Label lclock= new Label("CLK 5V");
	Label lR=new Label("0");
	Label lS=new Label("0");
	Label Q=new Label("Q=");
	Label noQ=new Label("/Q=");
	Label ltext=new Label("");
	boolean clock=false;
	
	public RS3()
	{
	}
	public String getAppletInfo()
	{
		return "Name: RS3\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(bR);
		add(lR);
		add(bS);
		add(lS);
		add(lclock);
		add(Q);
		add(noQ);
		add(ltext);
	}

	
	public void paint(Graphics g)
	{
		Color cl=g.getColor();
		g.setColor(Color.blue);
		g.drawString("Clocked RS Flip-Flop", 20,20);
		g.setColor(cl);
		//These are the only two values we need to define
		int dx=120;
		int dy=50;

		//placing all the gates invoking the method of version1 class
		ver1.coordenades(dx,dy);
		ver1.paint(g);
		g.drawLine(dx,dy+30,dx,dy+100);
		g.drawLine(dx-30,dy+65,dx-20,dy+65);
		g.drawLine(dx-20,dy+65,dx-20,dy+55);
		g.drawLine(dx-10,dy+55,dx-10,dy+65);
		g.drawLine(dx-10,dy+65,dx,dy+65);
		

		//making the truth table
		table1.coordenades(100,250);
		table1.q0="No Change";
		table1.q1="0";
		table1.q2="1";
		table1.q3="Not Allowed";
		table1.noq1="1";
		table1.noq2="0";
		table1.paint(g);

		//drawing the clock in red
		g.setColor(Color.red);
		if (clock) {g.drawLine(dx-23,dy+55,dx-7,dy+55);}
		else {g.drawLine(dx-23,dy+52,dx-7,dy+52);}

		//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
		lR.reshape(dx-20 , dy-10 , 20, 20);
		bR.reshape(dx-40 , dy-10 , 20, 20);
		lS.reshape(dx-20 , dy+120 , 20, 20);
		bS.reshape(dx-40 , dy+120 , 20, 20);
        lclock.reshape(dx-90 , dy+50 , 60, 20);
		Q.reshape(320 , 70 , 40, 20);
		noQ.reshape(320 , 142 , 40, 20);
		ltext.reshape(320,105,100,20);
		
	}




	/*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();
		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 bR and bS every time these buttons are pressed 
		if (e.target==bR) {
			if (lR.getText()=="0"){
				lR.setText("1");}
			else {
				lR.setText("0");}
			}
		
		if (e.target==bS) {
			if (lS.getText()=="0"){
				lS.setText("1");}
			else {
				lS.setText("0");}
			}
        return true;
    }

	public boolean mouseDown(Event evt, int x, int y)
	{
		//changing the output everytime the clock is pressed
		if ((x<113) & (x>97) & (y<115) & (y>90) ){
			clock=true;
			repaint();
			if ((lR.getText()=="0")&(lS.getText()=="0"))
			{
				ltext.setText("No Change");
			}

			else if ((lR.getText()=="0")&(lS.getText()=="1"))
			{
				Q.setText("Q=0");
				noQ.setText("/Q=1");
				ltext.setText("");
			}
			
			else if ((lR.getText()=="1")&(lS.getText()=="0"))
			{
				Q.setText("Q=1");
				noQ.setText("/Q=0");
				ltext.setText("");
			}

			else if ((lR.getText()=="1")&(lS.getText()=="1"))
			{
				Q.setText("Q=");
				noQ.setText("/Q=");
				ltext.setText("Not Allowed");
			}
		}

		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;
	}




}

