import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;


/**
 * In this game, the user is presented with an array of buttons that are 
 * initialized randomly to either black or white. Clicking on a button 
 * changes the color of the button and its two neighbors, if they exist. 
 * The object of the game is to reach a state when the buttons are all 
 * the same color. If this happens, something signals the win (how is 
 * up to you). Finish the code!
 *
 * @author Matt Boutell.
 */
public class TogglerFrame extends JFrame {
	
	private Button[] buttonList;
	/**
	 * Starts here.
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		int nButtons = 7; // could vary, of course!
		TogglerFrame frame = new TogglerFrame(nButtons);
		frame.setVisible(true);
	}

	/**
	 * Create a frame with the given number of buttons in it. 
	 * You are given the typical frame code to start.
	 * 
	 * @param nButtons The number of buttons to draw.
	 */
	public TogglerFrame(int nButtons) {
		this.setTitle("Toggler, by Matt Boutell");
		this.setSize(new Dimension(800, 100));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		
		this.buttonList = new Button[nButtons];
		
		for (int i = 0; i < nButtons; i++){
			this.buttonList[i] = new Button(); //MB
			this.add(this.buttonList[i]);
			if (i != 0 || i != nButtons - 1){
				this.buttonList[i].addActionListener(this.buttonList[i+1]);
				this.buttonList[i].addActionListener(this.buttonList[i-1]);
			} else if (i==0){
				this.buttonList[i].addActionListener(this.buttonList[i+1]);
			} else if (i==nButtons-1){
				this.buttonList[i].addActionListener(this.buttonList[i-1]);
			}

			
			
		}
	}

}
