/*
================================================
		FLIPOR.JAVA

  RS Flip-Flop Using NOR gates
================================================
*/
import java.applet.*;
import java.awt.*;

public class FlipOr extends Applet
{
	
	//creating new objects that are instances of classes
	dNor	nor1=	new dNor(this);
	dNor	nor2=	new dNor(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 Button bGo= new Button("Go");
	Label lR=new Label("0");
	Label lS=new Label("0");
	Label Q=new Label("Q=");
	Label noQ=new Label("/Q=");
	Label text=new Label("");

	//information about the applet
	public String getAppletInfo()
	{
		return "Name: FlipOr\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(bS);
		add(bGo);
		add(lR);
		add(lS);
		add(Q);
		add(noQ);
		add(text);
    
    }

	public void paint(Graphics g)
	{	
		Color cl=g.getColor();
		g.setColor(Color.blue);
		g.drawString("RS Flip-Flop Using NOR gates", 20,20);
		g.setColor(cl);
		//placing the gates
		nor1.coordenades(100,80);
		nor1.paint(g);
		nor2.coordenades(100,150);
		nor2.paint(g);
		table1.coordenades(100,250);
		//making the truth table
		table1.q0="No Change";
		table1.q1="1";
		table1.q2="0";
		table1.q3="Impossible";
		table1.noq1="0";
		table1.noq2="1";
		table1.paint(g);

		g.drawLine(110,110,110,130);
		g.drawLine(110,130,190,130);
		g.drawLine(190,130,190,165);

		g.drawLine(110,150,110,135);
		g.drawLine(110,135,185,135);
		g.drawLine(185,135,185,95);

		//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(80 , 70 , 30, 20);
        lS.reshape(80 , 170 , 30, 20);
		Q.reshape(200 , 85 , 40, 20);
		text.reshape(240 , 120 , 220, 20);
		noQ.reshape(200 , 155 , 40, 20);
		bR.reshape(60 , 70 , 20, 20);
		bS.reshape(60 , 170 , 20, 20);
		bGo.reshape(220 , 120 , 20, 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();
		//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 bR and bS every time these buttons are pressed 
		if (e.target==bR) {
			if (lR.getText()=="0"){
				lR.setText("1");}
			else {
				lR.setText("0");}
			}
		else if (e.target==bS){
			if (lS.getText()=="0"){
				lS.setText("1");}
			else {
				lS.setText("0");}
		}
			
		//changing the output everytime the button "GO" is pressed
		else if (e.target==bGo){
			
			if (lR.getText()=="0" & lS.getText()=="1")
			{
				Q.setText("Q=1");
				noQ.setText("/Q=0");
				text.setText("");
			}
			
			else if (lR.getText()=="1" & lS.getText()=="0")
			{
				Q.setText("Q=0");
				noQ.setText("/Q=1");
				text.setText("");}
			else if (lR.getText()=="0" & lS.getText()=="0"){
				
				text.setText("No Change");}

			else if (lR.getText()=="1" & lS.getText()=="1"){
				Q.setText("Q=");
				noQ.setText("/Q=");
				text.setText("Impossible");}
			
		}

		
        return true;
    }



	
}



	
