import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

/**
 * The JFrame which contains a panel upon which a Sierpinski Triangle is drawn.
 * It also contains and responds to buttons that, when pressed,
 * increase/decrease the level of detail of the Sierpinski Triangle.
 *
 * @author David Mutchler.
 *         Created October 18, 2008.
 */
public class SierpinskiFrame extends JFrame implements ActionListener {
	private final int panelWidth = 800;
	private SierpinskiPanel sierpinskiPanel;
	private JButton moreDetail;
	private JButton lessDetail;
	
	/**
	 * TODO Put here a description of what this constructor does.
	 *
	 */
	public SierpinskiFrame() {
		this.setSize(new Dimension(
				200 + this.panelWidth,
				50 + (int) (this.panelWidth * Math.sqrt(3.0) / 2.0)));
		this.setLayout(new FlowLayout());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.moreDetail = new JButton("Show more detail");
		this.lessDetail = new JButton("Show less detail");
		
		this.moreDetail.addActionListener(this);
		this.lessDetail.addActionListener(this);
		
		this.sierpinskiPanel = new SierpinskiPanel(this.panelWidth);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout());
		buttonPanel.setPreferredSize(new Dimension(150, 150));
		buttonPanel.add(this.moreDetail);
		buttonPanel.add(this.lessDetail);
		
		this.add(buttonPanel);
		this.add(this.sierpinskiPanel);
	}

	/**
	 * Increases or decreases the level of detail for the Sierpinski Triangle
	 * drawn in the panel.
	 * 
	 * @param event The event which invoked this method: pressing the
	 *              moreDetail button or the lessDetail button
	 */
	public void actionPerformed(ActionEvent event) {
		if (((JButton) event.getSource()) == this.moreDetail) {
			this.sierpinskiPanel.increaseLevelOfDetail();
		} else {
			this.sierpinskiPanel.decreaseLevelOfDetail();
		}
		this.sierpinskiPanel.repaint();
	}
}