/*
============================================================
			pruyecto.java

  Main: Transformation from Decimal to Binary Representation
============================================================
*/
import java.awt.*;
import java.applet.*;

public class proyecto extends Applet 
{
	int divident=345;
	TextField tf=new TextField("345");
	String decimal_value = new String();
	private Button b1= new Button(" To Binary ");
	Label label = new Label("Decimal? (>1,<511)");
	String divident_c = new String();

	public void init()
	{
		add(label);
		add(tf);
		add(b1);
	}

	public void paint(Graphics g)
	{
		//to draw the transformation the lenght and width of the numbers are taking into account
		decimal_value = tf.getText();
		divident_c=decimal_value;
		int advance_width_divisor, advance_width_divident, a_w_t, h, a;
		Font font = g.getFont();
		FontMetrics fontMetrics = g.getFontMetrics();
		int size = font.getSize();
		String name = font.getName();
		String divisor_c, quotient_c;
		String remainder_c;
		String binary_value=""; 

		label.move(40,20);
		tf.move(150,20);
		b1.move(200,20);


		int style = font.getStyle();
	  	int divisor=2, quotient, remainder, divident, X=50, Y=60;
		h=fontMetrics.getHeight();		
		a=fontMetrics.getAscent();
		divident = Integer.valueOf(divident_c).intValue();
		quotient=divident/divisor;
		divisor_c=Integer.toString(divisor);
		advance_width_divisor=fontMetrics.stringWidth(divisor_c);	
		int first_X=50;
		int first_Y=60;
		int i=0;
		while  (quotient!=0)
		{ 	
		quotient=divident/divisor;
		remainder=divident-(quotient*divisor);			
		remainder_c=Integer.toString(remainder);
		binary_value=remainder_c+binary_value;
		divident_c = new String();
		divident_c = Integer.toString(divident);
		advance_width_divident=fontMetrics.stringWidth(divident_c);
		

		int x1=X;
		int x5=X+advance_width_divisor;
		int x2=x5+5;
		int x3=x2+5;
		int x4=x3+advance_width_divident+5;
		int x6=180;
		int x7=x6+20;
		//int x8=x7+20;
		X=x5;
		int y1=Y;
		int y2=y1+1;
		int y3=y1+h;
 		int y4=Y-a-1;
		Y=y3;
		
	
		g.drawString(divisor_c, x1, y1);	
		g.drawLine(x2, y4, x2, y2);
		g.drawLine(x2, y2,x4, y2);
		g.drawString(divident_c, x3, y1);
		g.drawString(remainder_c, x6, y3);
		divident=quotient;
		divident_c=Integer.toString(divident);
		
		}//end of while loop

		//Print the last quocient after the while loop
		g.drawString("0", X+advance_width_divisor+10, Y);	
		
		//Print the final value	
		//So I need to store the first divident and all the remainders in a array.
		//The result is wrong, it's backwards.
		String text="Decimal:";
		a_w_t=fontMetrics.stringWidth(text);
		g.drawString(text, X,Y+20);
		g.drawString(decimal_value, X+a_w_t+20, Y+20);
		g.drawString("Binary:", X, Y+40);
		g.drawString(binary_value, X+a_w_t+20, Y+40);

		//Print the arrow 
		g.drawLine(220, first_Y, 220, Y);
		g.drawLine(217,first_Y+3,220,first_Y);
		g.drawLine(220,first_Y,223,first_Y+3);
		g.drawString(".", 180,first_Y);
		
		
		
		 
	}


	public boolean action(Event e, Object obj)
	{
		if (e.target == b1){
			decimal_value = tf.getText();
			divident_c=decimal_value;
			repaint();
		}
		return true;
		}


}

