/* normalTarget.java */

/**
* normalTarget.java
*/

import java.awt.*;
import java.applet.*;
import java.util.*;

public class normalTarget extends Applet {
	targetCanvas target;
	histogramCanvas xhistogram, yhistogram, xySquaredHistogram;
	int xbins, ybins;
	int[] xcount, ycount, xSquaredCount, ySquaredCount, xySquaredCount;
	double x,y;
	targetControlPanel controlPanel= new targetControlPanel(this);
	Panel hp = new Panel();   //panel for histograms (Insets?)
	Random generator = new Random();
	int sumX = 0;
	int  sumXsquared=0;
	int sumY = 0;
	int  sumYsquared = 0;
	double covariance=0;

	public void init () {
		xbins = 21;
		ybins  =21;
		xcount  = new int[xbins];
		ycount = new int[ybins];
		xSquaredCount = new int[xbins];
		ySquaredCount = new int[ybins];
		xySquaredCount = new int[xbins];
		target = new targetCanvas(this);
		xhistogram = new histogramCanvas(xcount, xbins, 2,this);
		yhistogram = new histogramCanvas(ycount, ybins, 2, this);
		xySquaredHistogram = new histogramCanvas(xySquaredCount, xbins, 
					2, this);
		BorderLayout bl = new BorderLayout();
		setLayout(bl);
		add("South", controlPanel);
		add("Center", target);
		
		GridLayout hpl = new GridLayout(3,1,2,4);
		hp.setLayout(hpl);
		hp.add(xhistogram);
		hp.add(yhistogram);
		hp.add(xySquaredHistogram);
		xhistogram.setBackground(new Color(0, 50, 250));
		yhistogram.setBackground( new Color(50, 0, 200));
		xySquaredHistogram.setBackground(new Color(100,100,0));
		add("East", hp);
		target.setBackground(Color.black);
	}

	public void play (int repetitions) {
		int xindex, yindex, xSquaredIndex, ySquaredIndex, 
							xySquaredIndex ;
		int height, width;
		height = target.getHeight();
		width = target.getWidth();
		for (int i=0; i< repetitions; i++ ) {
			x = generator.nextGaussian();
			y = generator.nextGaussian();
			x = 1/Math.sqrt(1+covariance*covariance)*(x- covariance*y);
			sumX += x;
			sumY += y;
			sumXsquared += x*x;
			sumYsquared += y*y;
			xindex = scale(x);
			yindex = scale(y);
			xSquaredIndex = scaleSquares(x*x);
			ySquaredIndex = scaleSquares(y*y);
			xySquaredIndex = scaleSquares(x*x+y*y);
			target.plot(x,-y); // y increases downwards
			xcount[xindex]++;
			ycount[yindex]++;
			xSquaredCount[xSquaredIndex]++;
			ySquaredCount[ySquaredIndex]++;
			xySquaredCount[xySquaredIndex]++;
			xhistogram.update(xcount);
			yhistogram.update(ycount);
			xySquaredHistogram.update(xySquaredCount);
			controlPanel.xc.update(x);
			controlPanel.yc.update(y);
		}
	}

	public void clear(double covariance) {
		this.covariance=covariance;
		xcount  = new int[xbins];
		ycount = new int[ybins];
		xySquaredCount = new int[xbins];
		xhistogram.clearHistogram(xcount, xbins);
		yhistogram.clearHistogram(ycount, ybins);
		xySquaredHistogram.clearHistogram(xySquaredCount,xbins);
		target.clear();
		}
		

	private int scale(double x ) {  // Should redo to match xbins !
		int result;
		if ( x < -4.0) {
			result = 0;
			}
		else if ( x > 4.0) {
			result = 20;
			}
		else {
			result = (int)(2.5*x+10);
			}
		return  result;
		}

	
	private int scaleSquares(double x ) {  // Should redo to match xbins !
		int result;
		result = (int)Math.min(3*x,20);
		return  result;
		}
}



