/**
HelloNewEvent.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.
  
  This applet uses the Java 1.1 event model.
*/
import java.io.*; 
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class HelloNewEvent extends Applet //Name the Hello13 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.
		ActionListener iListen =  new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				onClicked(); //Do this when the button is clicked
			}
		};
		
ActionListener iListenToo =  new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				onClicked(); //Do this when someone hits ’enter’ key while editing the textbox
			}
		};
		
		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
			setBackground(Color.white); //Make the applet background a nice color
			Enter.addActionListener(iListen);	//Add a listener to the button
			inputField.addActionListener(iListenToo);//And also to the textbox
		}

		
		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 void onClicked() //What to do when the enter
		{												//button is pressed or enter key is typed.
			char cTester;
			int iLen, iLenLess, iCounter=0;

			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;
			}
			if ( sNum.charAt(0) == ’-’ && iLen > 1 //Is the first character in the box a minus sign? If so, is there a second character?
				&&  (cTester = sNum.charAt(1)) > (’0’- 1) && cTester < (’9’ + 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 < ’0’ || cTester > ’9’) 
				{
					iFlag = 4;
					repaint();
					return;
				}
			}
			if(iLenLess > 4) //Is the string in the textbox too long
			{
				iFlag = 5;
				repaint();
				return;
			}
			iNum = Integer.parseInt(sNum); //Convert string to integer.
			if (iNum < 10) //Is the number too small?
			{
				iFlag=3;
				repaint();
				return;
			}
			if (iNum > 70) //Is the number too large?
			{
				iFlag=2;
				repaint();
				return;
			}
			iFlag=1; //The number is just right.
			repaint();
			return;
		}
}