/*
 * Created on Jan 31, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @author taylor
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ButtonGrid extends JFrame implements ActionListener {

    private static final int FRAME_WIDTH = 300;
    private static final int FRAME_HEIGHT = 100;
    private static final int FRAME_X_ORIGIN = 300;
    private static final int FRAME_Y_ORIGIN = 100;
    protected JButton[] buttons;
    protected final Color[] colors = {Color.white, Color.green, Color.blue, Color.yellow};
    
    public static void main(String[] args) {
        ButtonGrid grid = new ButtonGrid();
        grid.setVisible(true);
    }
    
    public ButtonGrid() {
        
        setTitle("Button Grid Example");
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setResizable(true);
        setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
        
        Container contentPane = getContentPane();
        contentPane.setBackground(Color.white);
        contentPane.setLayout(new GridLayout(1,3));
        
        buttons = new JButton[3];
        buttons[0] = new JButton();
        buttons[0].setActionCommand("0");
        buttons[1] = new JButton();
        buttons[1].setActionCommand("1");
        buttons[2] = new JButton();
        buttons[2].setActionCommand("2");
        
        // Register as a action event listener
        buttons[0].addActionListener(this);
        buttons[1].addActionListener(this);
        buttons[2].addActionListener(this);
        
        contentPane.add(buttons[0]);
        contentPane.add(buttons[1]);
        contentPane.add(buttons[2]);
                
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
    }
    
    public void actionPerformed(ActionEvent event) {
        
        int button = Integer.parseInt(event.getActionCommand());
        buttons[button].setBackground(colors[(int)(Math.random()*colors.length)]);
    }
  
}
