package buffon;

import java.awt.*;
import java.util.Vector;
import java.util.Enumeration;
import javax.swing.*;
import java.awt.geom.*;

/**
* BuffonPanel displays the Buffon Needle simulation
* @author Charles S. Stanton
* @version Mon Jul 08 12:08:04 PDT 2002
*/
class BuffonPanel extends JPanel{
		Dimension r;
		//plank dimensions of floor
		//floorWidth is determined by floorHeight and
		// displayed aspect ratio.
		float floorWidth;
		float floorHeight=4.0F;
		float needleLength=1.0F;
		Point2D.Float end1, end2;
		float[] lineEnds;
		Vector v;

		public BuffonPanel(float aNeedleLength){
		needleLength = aNeedleLength;
		setBackground(Color.white);
		}
		
		public Dimension getPreferredSize(){
			return new Dimension( 200, 200);
			}

		public Dimension getMinimumSize(){
			return new Dimension( 200, 200);
			}

		public void paint(Graphics g) {
			Graphics2D g2d = (Graphics2D)g;
			setBackground(Color.white);
		r = getSize();
		
		
		g.setColor(new Color(245, 245, 245));
        g.fillRect(0,0,r.width,r.height);
		g2d.setPaint(Color.black);
		Line2D.Float[] plankLine = new Line2D.Float[(int)Math.floor(floorHeight)];
		for (int i=0; i< plankLine.length; i++){
			plankLine[i] = new Line2D.Float(0, scaleY(i),r.width,scaleY(i));
			g2d.draw(plankLine[i]);
		}
		if (v != null){
			for (Enumeration e= v.elements();
				e.hasMoreElements(); ) {
					// elements of v should be end1.X, end1.Y, end2.X, end2.Y
					// scaled by plank width
				lineEnds = (float[])e.nextElement();
				
				if (Math.floor(lineEnds[1]) != Math.floor(lineEnds[3]) ) 
				{
				g2d.setPaint(Color.red);
				}
			else { g2d.setPaint(Color.blue);}
				g2d.draw(new Line2D.Float(scaleX(lineEnds[0]), scaleY(lineEnds[1]),
								scaleX(lineEnds[2]), scaleY(lineEnds[3])));
			}
		}
		else {
			System.out.println("null vector");
		}
}

	float scaleY(float y){
		float fy =r.height-r.height/floorHeight*y;
		return fy;
	}
	
	float scaleX(float x){
		float fx = (float)(r.height/floorHeight*x);
		return fx;
	}
	
	public void updateV(Vector av){
		v = av;
		super.repaint();
	}
	
	public double getAspectRatio(){
		return ((double)r.width)/(double)r.height;
	}

				
}
