/* 
 * Joe Schmoe 
 * testAverage.java
 * Find average of 3 input test scores
 */
import java.applet.Applet;   
import java.awt.*;		// abstract window toolkit (awt)
import java.awt.event.*;	// for event handlers
import javax.swing.*;    	// swing toolkit

public class testAverage extends JApplet {

        // TextField can also be JTextField (as in textbook)
	TextField test1 = new TextField ();	  	// input for test 1
	TextField test2 = new TextField ();	  	// input for test 2
	TextField test3 = new TextField ();	  	// input for test 3
	String testScore1,testScore2,testScore3; 	// values for tests
	double average;				 	// test score average

	// display test average
	public void paint (Graphics grafObj) {
		setBackground (Color.yellow);		// set applet background
	        grafObj.clearRect(0, 0, 400, 400); 	// clear previous text
		Font myFont = new Font ("Helvetica", Font.PLAIN, 18);		
		grafObj.setFont (myFont);
		grafObj.drawString ("Your average is "+average, 10, 120);
	}

	// set up the test GUI
	public void init () {
	        Container myContainer;
	    	
		// create a panel and objects to put into panel
		Panel thePanel = new Panel();
		// Label can also be JLabel (as in textbook)
		Label test1Label = new Label ("Test 1 score: ");
		Label test2Label = new Label ("Test 2 score: ");
		Label test3Label = new Label ("Test 3 score: ");

		// set up how panel will look
		thePanel.setLayout (new GridLayout (3, 2));
		
		thePanel.setBackground (Color.yellow);

		// put stuff onto panel
		thePanel.add (test1Label);
   	        thePanel.add (test1);
		thePanel.add (test2Label);
	        thePanel.add (test2);
		thePanel.add (test3Label);
	        thePanel.add (test3);
		test1.addActionListener (new TextListener());
		test2.addActionListener (new TextListener());
		test3.addActionListener (new TextListener());

		// add the panel itself to the container
		myContainer = getContentPane();
		myContainer.add (thePanel, BorderLayout.NORTH);
	}











	// get test scores and calculate average
	public class TextListener implements ActionListener {
		public void actionPerformed (ActionEvent event) {
			double score1, score2, score3;	// test scores

		   	// get test scores (as strings!!)
		   	testScore1 = test1.getText();
		   	testScore2 = test2.getText();
		   	testScore3 = test3.getText();

			// convert strings to double scores
			score1 = Double.valueOf (testScore1).doubleValue();
			score2 = Double.valueOf (testScore2).doubleValue();
			score3 = Double.valueOf (testScore3).doubleValue();

			// calculate average 
			average = (score1 + score2 + score3) / 3.0;
         		repaint();
		}
	}
}
