/*
 * Joe Schmoe 
 * ifStuff.java
 * Program to show IF statements; also has two buttons.
 */

import java.applet.Applet;   
import java.awt.*;		// abstract window toolkit (awt)
import java.awt.event.*;	// for event handlers
import javax.swing.*;

public class ifStuff extends JApplet {
	TextField myField = new TextField (2);	// place to type in number
	String inputStr;			// variable to hold number
	int intNumber;				// number converted to integer

	// determine if number is even/odd and display result
	public void paint (Graphics grafObj) {
		Font myFont = new Font ("Helvetica", Font.PLAIN, 18);
		grafObj.setFont (myFont);
                grafObj.clearRect (0, 0, 400, 400);

		if (intNumber != 0) {
			if (intNumber % 2 == 0)
				grafObj.drawString(intNumber+" is an EVEN number",10,120);
			else
				grafObj.drawString(intNumber+" is an ODD number",10,120);
		}
	}

	// set up the test GUI
	public void init () {	
		// create panel and objects to put into panel
		Panel thePanel = new Panel();
		Label myLabel = new Label ("Enter a number: ");
		Button clearButton = new Button ("Clear");
		Button submitButton = new Button ("Submit");

		// set up how panel will look
		thePanel.setBackground (Color.gray);
		thePanel.setLayout (new GridLayout (2, 2, 10, 20));

		// put stuff onto panel
		thePanel.add (myLabel);
		thePanel.add (myField);
		thePanel.add (clearButton);
		thePanel.add (submitButton);

		// add action listeners (methods) to each button
		clearButton.addActionListener (new doClear ());
		submitButton.addActionListener (new doSubmit ());

		// add the panel itself to the Java applet
		Container myContainer;
		myContainer = getContentPane();
		myContainer.add (thePanel, BorderLayout.NORTH);
		
		// hmmm, why is this here?
		intNumber = 0; 
	}

	// clears text field and reset number to 0
	public class doClear implements ActionListener {
		public void actionPerformed (ActionEvent event) {
			intNumber = 0;
			myField.setText(" ");
			repaint ();
		}
	}

	// get text and convert to number
	public class doSubmit implements ActionListener {
		public void actionPerformed (ActionEvent event) {
			double number;		// input number

			// get input
		   	inputStr = myField.getText();

			// convert to number (double)
			number = Double.valueOf (inputStr).doubleValue();
			// convert to integer
			intNumber = (int) number;

         	        repaint();
		}
	}
}
