package buffon;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.text.NumberFormat;

/**  
* This is the control panel for the lotto Class   		*
* @author Charles Stanton					*
* @version July 8, 2002					*
*/
public class BuffonControlPanel extends JPanel{
	Font f = new Font("TimesRoman", Font.BOLD,12);
	
	JComboBox ch;
	double X; //ratio of hits
	double r; //ratio 2/X
	NumberFormat nf;
	
	double  theta;
	double needleLength;
	int crossings = 0; 		//counter for # of crossings.
	int plays=0; 			//total number of plays
	double floorWidth, floorHeight;

	BuffonPanel floor;
	//Line2D line;
	Vector v = new Vector();	//vector of 
	float[] lineEnds;
	
	int n_repetitions=1;
	JLabel xLabel = new JLabel("X = ");
	JTextField xField = new JTextField(8);
	JLabel rcLabel = new JLabel("2/X = ");
	JTextField rcField = new JTextField(8);

	public BuffonControlPanel(BuffonPanel aFloor){
		floor = aFloor;
		
		//this.applet=applet;
		nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(5);
		setBackground(new Color(230,230,230));
		setLayout(new FlowLayout());
		String[] chString = {"1", "2", "10", "20"};
		ch = new JComboBox(chString);
		add(ch);
		floorWidth = 12.0;
		floorHeight= 4.0;
		JButton playButton = new JButton("play");
		add(playButton);
		playButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//Integer.parseInt(itemLabel)
				n_repetitions = Integer.parseInt((String)ch.getSelectedItem());
				play(n_repetitions);
			}
		});
		JButton clearButton = new JButton("Clear");
		clearButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
		    clear();
			}
		}); 
		add(clearButton);
		add(xLabel);
		add(xField); 
		add(rcLabel);
		add(rcField);
	}


public void play( int repetitions) {
		needleLength = 1.0;
		floorWidth = floor.getAspectRatio()*floorHeight;
		double centerX, centerY,  xOff, yOff;
		
		for ( int i=0; i< repetitions; i++) {
			lineEnds = new float[4];
			plays++;
			//center of needle is vertically distributed uniformly between
			// 0.5 and floorHeight-0.5
			centerY = 0.5+(floorHeight-1.0)*Math.random();
			
			//center of needle is horizontally distributed uniformly between
			// 0.5 and floorWidth-0.5
			centerX= 0.5+(floorWidth-1.0)*Math.random();
			//angle to upper end is uniformly distributed on [0, Pi]
			theta = Math.PI*Math.random();
			xOff = 0.5*needleLength*Math.sin(theta);
			yOff = 0.5*needleLength*Math.cos(theta);
			lineEnds[0] = (float)(centerX+xOff);
			lineEnds[1] = (float)(centerY+yOff);
			lineEnds[2] = (float)(centerX-xOff);
			lineEnds[3] = (float)(centerY-yOff);
			v.addElement(lineEnds);
			if (Math.floor(lineEnds[1]) != Math.floor(lineEnds[3])) {
				crossings++;
				}
			
		}
		X = crossings/((double)plays);
		if (X != 0){ r = 2/X;}
		xField.setText(nf.format(X));
		rcField.setText(nf.format(r));
		floor.updateV(v);
	}


		public void clear() {
			v= new Vector();
			floor.updateV(v);
			floor.repaint();
			X=0.0;
		
			xField.setText(nf.format(X));
			rcField.setText("");
			crossings=0;
			plays=0;
			}


}

