/**
Hello11.java

  This applet lets one repeatedly enter a number
  and then prints out four strings on separate
  lines separated by a vertical distance specified
  by the number.

  The applet gives online instructions and helps the
  user correct errors made in selecting the numbers 
  and other character strings to input.
*/
import java.io.*; 
import java.awt.*;
import java.applet.*;

public class Hello11 extends Applet //Name the Hello11 class being described.
{
		int x=10, //x-coordinate
			y=50, //y-coordinate
			iNum, //input line-spacing variable
			iFlag=0; //shows what operation paint() will perform when called.
			final String  sOutString1="First", //Strings to be output at various times.
				sOutString2="Second",
                sOutString3="Third",
                sOutString4="Fourth",
                sPlease="Please enter a new spacing number between",
                sPlease2="10 and 70 and click the button.",
                sPlease3="-------------------------------",
				sErrorMsg1="The number was TOO LARGE.",
				sErrorMsg2="The number was TOO SMALL.",
				sErrorMsg3="Try again.",
				sErrorMsg4="Enter only numbers.",
				sErrorMsg5="You entered too many digits.",
				sErrorMsg6="You must enter something.",
				sUnexpected="Something very unexpected happened!!!",		
                sLabel="Enter a Number:"; //String for label for text entry box
			TextField inputField = new TextField(4); //Specify text entry box.
			Button Enter = new Button("Enter"); //Specify enter button.
		
		public void init() //Setup applet window at startup
		{
            add(new Label(sLabel)); //Draw text entry box label
			add(inputField); //Draw text entry box
            add(Enter); //Draw enter button
		}

		
		public void paint(Graphics gA) //Method to run when the applet window is first
		{							   //drawn or when the window is uncovered.
			switch (iFlag)			   //Choose with thing to do based on what flag says
			{
				case 1: //Do this when the number entered is ok and when it is not the 
				{		//first time paint() is called.
					x=10;
					y=50;
					gA.drawString(sPlease, x, y);
					y+=15;
					gA.drawString(sPlease2, x, y);
					y+=15;
					gA.drawString(sPlease3, x, y);
					y+=20;
					gA.drawString(sOutString1, x, y);
					y+=iNum;
					gA.drawString(sOutString2, x, y);
					y+=iNum;
					gA.drawString(sOutString3, x, y);
					y+=iNum;
					gA.drawString(sOutString4, x, y);
					y+=iNum;
					return;
				}
			
				case 2: //Do this when the number entered is too large.
				{
                    x=10;
                    y=50;
                    gA.drawString(sPlease, x, y);
                    y+=15;
                    gA.drawString(sPlease2, x, y);
                    y+=20;
                    gA.drawString(sErrorMsg1, x, y);
                    y+=15;
                    gA.drawString(sErrorMsg3, x, y);
                    y+=15;
                    gA.drawString(sPlease3, x, y);
					return;
				}
				
				case 3: //Do this when the number entered is too small.
				{
                    x=10;
                    y=50;
					gA.drawString(sPlease, x, y);
                    y+=15;
					gA.drawString(sPlease2, x, y);	
                    y+=20;
                    gA.drawString(sErrorMsg2, x, y);
                    y+=15;
                    gA.drawString(sErrorMsg3, x, y);
                    y+=15;
                    gA.drawString(sPlease3, x, y);
					return;
				}
				
				case 4: { //Do this when a non-number character is entered.
					x=10;
                    y=50;
                    gA.drawString(sPlease, x, y);
                    y+=15;
                    gA.drawString(sPlease2, x, y);
                    y+=20;
                    gA.drawString(sErrorMsg4, x, y);
                    y+=15;
                    gA.drawString(sErrorMsg3, x, y);
                    y+=15;
                    gA.drawString(sPlease3, x, y);
					return;
				}
				case 5: { //Do this when a non-number character is entered.
					x=10;
                    y=50;
                    gA.drawString(sPlease, x, y);
                    y+=15;
                    gA.drawString(sPlease2, x, y);
                    y+=20;
                    gA.drawString(sErrorMsg5, x, y);
                    y+=15;
                    gA.drawString(sErrorMsg3, x, y);
                    y+=15;
                    gA.drawString(sPlease3, x, y);
					return;
				}
				case 6: { //Do this when a non-number character is entered.
					x=10;
                    y=50;
                    gA.drawString(sPlease, x, y);
                    y+=15;
                    gA.drawString(sPlease2, x, y);
                    y+=20;
                    gA.drawString(sErrorMsg6, x, y);
                    y+=15;
                    gA.drawString(sErrorMsg3, x, y);
                    y+=15;
                    gA.drawString(sPlease3, x, y);
					return;
				}
				case 0: //Do this the first time paint() is called, when the
				{		//applet is started.
					x=10; 
					y=50;
					gA.drawString(sPlease, x, y);
                    y+=15;
					gA.drawString(sPlease2, x, y);
                    y+=15;
                    gA.drawString(sPlease3, x, y);
				}
				
				default: //Other cases, which should not exist.
				{
					return;
				}
			}
		}

		public boolean action(Event evt, Object what) //What to do when the enter
		{											  //button is pressed.
			char cTester;
			int iLen, iLenLess, iCounter=0;

			if (evt.target != Enter) return false;
			String sNum = inputField.getText(); //Read number string from text entry box.
			iLen=iLenLess=sNum.length(); //Set both length count variables
			if (iLen == 0) //Is the textbox empty?
			{
				iFlag=6;
				repaint();
				return false;
			}
			if ( sNum.charAt(0) == &#146;-&#146; && iLen > 1 //Is the first character in the box a minus sign? If so, is there a second character?
				&&  (cTester = sNum.charAt(1)) > (&#146;0&#146;- 1) && cTester < (&#146;9&#146; + 1) )  //Is the second character a number
			{
				iCounter =2; //Move index past the minus sign and the first number digit
				iLenLess--; //Make sure the minus sign is not counted in the length of the number part of the string
			}
			for( ; iCounter < iLen; iCounter++) //Are all the characters numbers?
			{									//This avoids an exception from non-numbers converted to a string
				cTester = sNum.charAt(iCounter);
				if (cTester < &#146;0&#146; || cTester > &#146;9&#146;) 
				{
					iFlag = 4;
					repaint();
					return false;
				}
			}
			if(iLenLess > 4) //Is the string in the textbox too long
			{
				iFlag = 5;
				repaint();
				return false;
			}
			iNum = Integer.parseInt(sNum); //Convert string to integer.
			if (iNum < 10) //Is the number too small?
			{
				iFlag=3;
				repaint();
				return false;
			}
			if (iNum > 70) //Is the number too large?
			{
				iFlag=2;
				repaint();
				return false;
			}
			iFlag=1; //The number is just right.
			repaint();
			return true;
		}
}