/*
=========================================================
			Fraction.Java

  Main: addition of two binary numbers
=========================================================
*/
import java.applet.*;
import java.awt.*;

public class fraction extends Applet
{
	
	private Button b3= new Button("+");
	private Button b2= new Button("+");
	private Button b1= new Button("+");

	Label label3 = new Label("0");
	Label label2 = new Label("0");
	Label label1 = new Label("0");
	Label label0 = new Label("0.");

	private Button bgo= new Button("GO>>");
	int figure3=0;
	int figure2=0;
	int figure1=0;
	int num, sizeNum, sicex2, sizeRead, sizeOut, carry;
	boolean go=false;
	String binario = new String("");
	String prefijo = new String("");

	public fraction()
	{
	}

	public String getAppletInfo()
	{
		return "Name: fraction\r\n" +
		       "Author: Ana Cristina Fernandez\r\n" +
		       "Created with Microsoft Visual J++ Version 1.1";
	}

	public void init()
	{
		add(b3);
		add(b2);
		add(b1);
		add(label3);
		add(label2);
		add(label1);
		add(label0);
		add(bgo);
	}

	public void paint(Graphics g)
	{	num=figure3*100+figure2*10+figure1;
		b3.move(40, 50);
		b2.move(60,50);
		b1.move(80,50);
		label3.move(45,70+10);
		label2.move(65,70+10);
		label1.move(85,70+10);
		label0.move(20,70+10);
		bgo.move(130,50);
		g.drawString("Introduce the decimal fraction and press the GO button",10,10);

		if (go==true){
			int sizeNum1, sizex2, h, a;
			Font font = g.getFont();
			FontMetrics fontMetrics = g.getFontMetrics();
			int size = font.getSize();
			String name = font.getName();
			int style = font.getStyle();
			h=fontMetrics.getHeight();		
			a=fontMetrics.getAscent();
			if ((num<1000)&(num>99)){
				prefijo=".";}
			else if ((num<100)&(num>9)){
				prefijo=".0";}
			else if ((num<10)&(num>0)){
				prefijo=".00";}
			sizeNum=fontMetrics.stringWidth(prefijo+num);
			sizex2=fontMetrics.stringWidth("x 2");	

			int X=400;
			int Y=10;

			int x1=X;//arrow
			int x2=x1+20;//carry
			int x3=x2+20;//line
			int x4=x3+20;//num
			int x5=x4+sizeNum-sizex2;//x2
			int x6=x4+sizeNum+5;//end line

			int decimal=num;//decimal is not modified

			int y1=Y;//num
			int i=0;

			while ((num!=0) & (i<8)){
				i=i+1;
				int y2=y1+h;//x2
				int y3=y1+h+5;//line
				int y4=y3+a;//next num
				
				g.drawString(prefijo+num,x4,y1);
				g.drawString("x 2",x5,y2);
				g.drawLine(x3,y3,x6,y3);

				num=num*2;
				carry=0;
				if (num>999){
					carry=1;
					num=num-1000;}
				else if ((num<1000)&(num>99)){
					prefijo=".";}
				else if ((num<100)&(num>9)){
					prefijo=".0";}
				else if ((num<10)&(num>0)){
					prefijo=".00";}

				binario=binario+""+carry+"";

				g.drawString(""+carry+"", x2, y4);

				y1=y4;
				
			}//end while loop
				
				g.drawString(prefijo+num+"", x4, y1);
				g.drawLine(X,Y,X,y1);//line
				g.drawLine(X-2,y1-2,X,y1);//arrow
				g.drawLine(X+2,y1-2,X,y1);//arrow
				g.drawString(".", x2, Y);//decimal point
				sizeRead=fontMetrics.stringWidth("Read in this direction");	
				g.drawString("Read in this direction", X-sizeRead-5, y1);//decimal point
				
				if ((decimal<1000)&(decimal>99)){
					prefijo="0.";}
				else if ((decimal<100)&(decimal>9)){
					prefijo="0.0";}
				else if ((decimal<10)&(decimal>0)){
					prefijo="0.00";}
				sizeOut=fontMetrics.stringWidth(prefijo+decimal+"(decimal) = 0."+binario+"(binario)");
				g.drawString(prefijo+decimal+"(decimal) = 0."+binario+"(binary)", X-sizeOut-5, (y1+Y)/2);

				//getting binario empty to zero otherwise the bits are store at the end of the previous number
				
				
				binario="";




		}

	}


	public boolean action(Event e, Object obj)
	{
		if (e.target==b3){
			figure3=figure3+1;
			if (figure3==10){figure3=0;}
			label3.setText(""+figure3+"");}
		if (e.target==b2){
			figure2=figure2+1;
			if (figure2==10){figure2=0;}
			label2.setText(""+figure2+"");}
		if (e.target==b1){
			figure1=figure1+1;
			if (figure1==10){figure1=0;}
			label1.setText(""+figure1+"");}
		if (e.target==bgo){
			num=figure3*100+figure2*10+figure1;
			go=true;
			repaint();}

		return true;
		}


}






	
