import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/* One way to handle an array of buttons, has other widgets */

public class ButtonArray extends Applet implements ActionListener
{
    //applet member data
    Button [] buttonArray;
    int row = 3;
    int col = 4;
    int totalButtons = row * col;
    //panels and other nonsense
       
    public void init ()
    {
       setLayout(new GridLayout(row,col,0,0));
	
	//declare a new array of buttons
	buttonArray = new Button [totalButtons];

	//initialize each of the buttons in the array
	//with an empty label
	for (int nNum = 0 ; nNum < totalButtons ; nNum++)
	{
	    buttonArray [nNum] = new Button ("");
	    add (buttonArray [nNum]);
	    buttonArray [nNum].addActionListener (this);
	    buttonArray [nNum].setBackground (Color.yellow);
	    //each button will send a message with its number
	    buttonArray [nNum].setActionCommand ("" + nNum);
	}
	
     
    }

    public void actionPerformed (ActionEvent e)
    {//To handle the button array
    
	//get the number of the button from getActionCommand
	//convert it to an int and store it in i
	int i = Integer.parseInt (e.getActionCommand ());

	//display button Number at bottom of screen
	//showStatus ("Button number " + i);

	//change background of buttons
	if (buttonArray [i].getBackground () != Color.red)
	    buttonArray [i].setBackground (Color.red);
	else
	    buttonArray [i].setBackground (Color.blue);
    }
}


