//-------------------------------------------------------------------------------------------------
//
// CalcComponent3.java
//
// written by:    Simon Parsons
// last modified: 7th September 2012
//

// A graphical object that does some very basic arithmetic operations. 
// 
// In its incomplete form, it allows the user to enter one number, 
// generates a second random number, and provides the means to 

import java.awt.*;                                                 // Use the awt library
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;                                              // Use the swing library
import java.util.Random;                                           // Get random numbers
import java.lang.Number.*;                                         // Need this for Integer

// The Moving Component class is a subclass of JComponent and defines an object that listens
// for actions (in this case multiple button presses).

public class CalcComponent3 extends JComponent 
	implements ActionListener
{   
	int a = 0;
	int b;     
	int result = 0;	
	JButton buttons[]; 
	
	JTextField aField;
	JTextField bField;
	JTextField rField;
	
	Random randomGenerator = new Random();    // Need this to generate random numbers.
	
	// Set up buttons and add a listener for each. The first four buttons (should)
	// implement some arithmetic operations, the last resets the calculator.
	
	public CalcComponent3() {
		
		// Set b to a random value (have to do this in a method) and use
		// GridLayout to lay the interface out in two columns.
		b = randomGenerator.nextInt(10);
		setLayout(new GridLayout(0,2)); 

		// Create buttons and add them
		buttons = new JButton[5];
		buttons[0] = new JButton("+");
		buttons[1] = new JButton("-");		
		buttons[2] = new JButton("*");
		buttons[3] = new JButton("/");
		buttons[4] = new JButton("Again!");
				for(int i=0; i<5; i++){
			add(buttons[i]);
			buttons[i].addActionListener(this);
		}	
		
		// Create fields and add them
		aField = new JTextField();
		aField.setColumns(3);
		add(aField);
		bField = new JTextField();
		bField.setColumns(3);
		add(bField);
		rField = new JTextField();
		rField.setColumns(3);
		add(rField);

		// Send values to the textfields
		display();
	}
		
	// Do the relevant operation on the numbers when a function key is 
	// hit.
	
	public void actionPerformed(ActionEvent e){       		
	
		// First, read the value of a from the textfield:
		String aString = aField.getText();
	    Integer aInt = new Integer(aString);
	    a = aInt.intValue();
	    
		// Now respond to buttons
	    // The first button is +, so add up the numbers
		if(e.getSource() == buttons[0]){
			addUp(a, b);
		}
		if(e.getSource() == buttons[1]){
			;
		}
		if(e.getSource() == buttons[2]){
			;
		}
		if(e.getSource() == buttons[3]){
			;
		}
		
		// This is the Again! button, so reset a, b and result.
		if(e.getSource() == buttons[4]){
			a = 0;
			b = randomGenerator.nextInt(10);
			result = 0;
		}

		// Now send values to the text fields
		display();
	}
	
	// A function to sum up two integers, writing the outcome into
	// the attribute result.
	
	public void addUp(int x, int y){
		result = x + y;
	}
	
	// Display the values of a, b and result in the relevant text fields
	
	public void display(){
		Integer aInt = a;
		aField.setText(aInt.toString());
		Integer bInt = b;
		bField.setText(bInt.toString());	
		Integer rInt = result;
		rField.setText(rInt.toString());
	}
}
