import java.applet.*;
import java.awt.*;
import java.lang.Integer;

public class Precidence extends Applet
{
    final int FONTSIZE = 18;
    final int OFFSET = FONTSIZE + 2;
    final int BASE = 100;

    Font fnorm = new Font("TimesRoman", Font.PLAIN, FONTSIZE);
    FontMetrics fm = getFontMetrics(fnorm);

    Font f1 = new Font("TimesRoman", Font.PLAIN, FONTSIZE - 4);
    Font f2 = new Font("TimesRoman", Font.PLAIN, FONTSIZE - 7);

    String b1, b2, b3, b4;
    String r1, r2, r3;
    String msg, msg2;

    int counter;
    int whichpath;

//**************************************************
 
    public void init()
    {
	counter=0;
	setBackground(Color.white);
	changeit();
	repaint();

    }

//*********************************************************
    public boolean mouseDown(Event e, int x, int y)
    {
	counter++;
	changeit();
	repaint();
	return true;
    }

//**********************************************************
 
    public void restart()
    {
	counter=0;
	b1 = b2 = b3 = b4 = r1 = r2 = r3 = "";
	msg = "";
	msg2 = "";
    }

//*************************************************************
        
    public void changeit()
    {
	switch(counter)
	{
	  case 0:
		msg = "When does it hit this??";	
		break;
	  case 1:
		//don't set any values, just go down and print intro
		break;
	  case 2:
                b1 = "ANSWER = (( X ** 2 ** 3 + 5 ) * Y - 14 + Z ** 3 ) / 2"; 
                r1 = "";
                b2 = "";
                r2 = ""; 
                b3 = ""; 
                r3 = ""; 
                b4 = ""; 
                msg = "Here is the same formula in 'computer-ese'.";
                break;

	  case 3:	
                b1 = "ANSWER = (( "; 
                r1 = "X ";
                b2 = "** 2 ** 3 + 5 ) * ";
                r2 = "Y "; 
                b3 = "- 14 + "; 
                r3 = "Z "; 
                b4 = "** 3 ) / 2 "; 
                msg = "First, substitute values in for variables.";
                break;

	  case 4:
		b1 = "ANSWER = (";
		r1 = "( ";
		b2 = "2 ** ";
		r2 = "2 ** 3 ";
		b3 = "+ 5 ";
		r3 = ") ";
		b4 = "* 3 - 14 + 6 ** 3 ) / 2 ";
		msg = "Work within the innermost parentheses.";
	 	msg2 = "Start on the rightmost exponent.";
		break;

          case 5:
                b1 = "ANSWER = (";
                r1 = "( 2 ** 8 ";
                b2 = "+ 5 ";
                r2 = ") ";
                b3 = "* 3 - 14 + 6 ** 3 ) / 2 ";
                r3 = "";
                b4 = "";
                msg = "Move to the next exponent.";
                break;
     
          case 6:
                b1 = "ANSWER = (";
                r1 = "( 256 + 5 ) ";
                b2 = "* 3 - 14 + 6 ** 3 ) / 2 ";
                r2 = "";
                b3 = "";
                r3 = "";
                b4 = "";
                msg = "Perform the addition inside of the parentheses.";
                break;
     
          case 7:
                b1 = "ANSWER = ";
                r1 = "( ";
                b2 = "261 * 3 - 14 + ";
                r2 = "6 ** 3 ) ";
                b3 = "/ 2 ";
                r3 = "";
                b4 = "";
                msg = "In next set of parentheses, perform exponentiation.";
                break;
     
          case 8:
                b1 = "ANSWER = ";
                r1 = "( 261 * 3 ";
                b2 = "- 14 + 216 ";
                r2 = ") ";
                b3 = "/ 2 ";
                r3 = "";
                b4 = "";
                msg = "Now, solve the multiplication.";
                break;
     
          case 9:
                b1 = "ANSWER = ";
                r1 = "( 783 - 14 ";
                b2 = "+ 216 ";
                r2 = ") ";
                b3 = "/ 2 ";
                r3 = "";
                b4 = "";
                msg = "Perform additions or subtractions starting at left.";
                break;
     
          case 10:
                b1 = "ANSWER = ";
                r1 = "( 769 + 216 ) ";
                b2 = "/ 2 ";
                r2 = "";
                b3 = "";
                r3 = "";
                b4 = "";
                msg = "Finish operation in parentheses.";
                break;
     
          case 11:
                b1 = "ANSWER = ";
                r1 = "985 / 2 ";
                b2 = "";
                r2 = "";
                b3 = "";
                r3 = "";
                b4 = "";
                msg = "Finish equation.";
                break;
     
          case 12:
                b1 = "ANSWER = ";
                r1 = "492.5 ";
                b2 = "";
                r2 = "";
                b3 = "";
                r3 = "";
                b4 = "";
                msg = "This is an integer equation, so this is truncated.";
                break;
     
          case 13:
                b1 = "";
                r1 = "ANSWER = 492 ";
                b2 = "";
                r2 = "";
                b3 = "";
                r3 = "";
                b4 = "";
                msg = "Put this number into the variable ANSWER,";
		msg2 = "and you're done.";
                break;
	  case 14:
		restart();	
		break;
	}	
    }

//*************************************************************
        
    public void origformula(Graphics g)
    {
	g.drawString("ANSWER =", BASE-30, 30+OFFSET/2);
	g.drawString("(X     + 5)  * Y - 14 + Z", BASE+70, 30);
	g.drawString("___________________", BASE+70, 30);
	g.drawString("2", BASE+150, 30+OFFSET);
	g.setFont(f1);
	g.drawString("2", BASE+92, 22);
	g.drawString("3", BASE+240, 22);
	g.setFont(f2);
	g.drawString("   3", BASE+94, 16);
	g.setFont(fnorm);

    }

//*************************************************************
    public void paint(Graphics g)
    {
	g.setFont(fnorm);
	
//each one of the colors started after stringWidth() of other

	origformula(g);

	if (counter == 1) {
	   g.drawString("Suppose you are given the above equation to calculate,", 0, 100);
	   g.drawString("where variable identifiers X, Y, Z, and ANSWER are", 0, 125);
	   g.drawString("all integers.  Let X = 2, Y = 3, and Z = 6, respectively.", 0, 150);
	   g.setColor(Color.red);
	   g.drawString("Click your left mouse button now to begin evaluating", 0, 225);
	   g.drawString("the expression.", 0, 250); 
	   g.setColor(Color.black); }
	else { 
	   g.drawString(b1, 0, 100);
	   g.drawString(b2, fm.stringWidth(b1) + fm.stringWidth(r1),100);
	   g.drawString(b3,  fm.stringWidth(b1) + fm.stringWidth(r1)
	      	+ fm.stringWidth(b2) + fm.stringWidth(r2), 100);
	   g.drawString(b4,  fm.stringWidth(b1) + fm.stringWidth(r1) 
		+ fm.stringWidth(b2) + fm.stringWidth(r2) + fm.stringWidth(b3) 
		+ fm.stringWidth(r3), 100);

	   g.setColor(Color.red);

	   g.drawString(r1, fm.stringWidth(b1), 100);
	   g.drawString(r2, fm.stringWidth(b1) + fm.stringWidth(r1)       
                + fm.stringWidth(b2), 100);
	   g.drawString(r3, fm.stringWidth(b1) + fm.stringWidth(r1)      
                + fm.stringWidth(b2) + fm.stringWidth(r2) + fm.stringWidth(b3),
		100);

	   g.setColor(Color.black);
	   g.drawString(msg, 0, 200);
	   if (msg2 != "") {
		g.drawString(msg2, 0, 225);
		msg2 = "";
	   }
	} // end else

	//try { Thread.sleep(1000); }
	//catch (InterruptedException e) {}
	//g.drawString(b1+r1+b2+r2+b3+r3+b4, 0, 100);
    }
}


