import java.awt.*;

// Written by Julian Devlin, 8/97, for the text book
// "Introduction to Probability," by Charles M. Grinstead & J. Laurie Snell

public class ChiSquareGraph extends Graph {

	Float xLine[];				// Second set of coordinates
	Float yLine[];				
	Point pixelsLine[];				// pixel points for second values
	int lengthSpike;
	boolean hiliteOn;
	float beginHilite, endHilite;
	int lineLength;

	public ChiSquareGraph() {
		super();
		lengthSpike = 0;
		xLine = new Float[1];
		xLine[0] = new Float(0);
		yLine = new Float[1];
		yLine[0] = new Float(0);
		hiliteOn = false;
	}
	
	public ChiSquareGraph(Float[] x, Float[] y) {
		super(x, y);
	}
	
	// ONLY WORKS FOR 5 DEGREES OF FREEDOM!!!!!
	// Determines size of this object, and scales the coordinates accordingly.
	public void setPoints() {
		super.setPoints();
		
		lineLength = w / 4;
		xLine = new Float[lineLength];			// Set up arrays for x and y coordinates of data points
		yLine = new Float[lineLength];
		
		for(int i = 0; i < lineLength; i++) {
			xLine[i] = new Float(minX +  i * (float) (maxX - minX) / ((float) lineLength));
			int n = 5;
			double gamma = 1.329340388;
			if (xLine[i].floatValue() <= 0)
				yLine[i] = new Float(0);
			else 
				yLine[i] = new Float(Math.pow((double) xLine[i].floatValue(), (double)(n - 2) / 
					2.0) * Math.pow(Math.E, -1 * (double) xLine[i].floatValue() / 2) / (
					 Math.pow(2, (double) n / 2) * gamma));	
		}
		
		pixelsLine = new Point[lineLength];			// Initialize array for actual points to be graphed
		
		int x, y;									// current x and y coordinates
		for (int i = 0; i < lineLength; i++) {
			// Calculate what the coordinates of each data point should be, then add a Point object to the array
			x = adjX(xLine[i]);
			y = adjY(yLine[i]);
			pixelsLine[i] = new Point(x, y);
		}
	}
	
	public void paint(Graphics g) {
		Font f;
		String fName;
		FontMetrics fm;
	
		if (length > 0) { 					// Make sure we have at least one point
			f = g.getFont();
			fName = f.getName();
			f = new Font(fName, Font.PLAIN, 9);
			g.setFont(f);
			fm = g.getFontMetrics();
		
			setPoints();				// When we are ready to paint, the dimensions of the component have been
			setTicks(false, fm);			// set, so we can calculate coordinates of all the points
			
			g.setColor(new Color(150, 0, 0));
			for (int i = 0; i < lineLength - 1; i++) {		// For each point
				g.drawLine(pixelsLine[i].x, pixelsLine[i].y, pixelsLine[i + 1].x,
					pixelsLine[i + 1].y);				// Connect it to the next point
			}
			
			g.setColor(new Color(0, 0, 0));
			for (int i = 0; i < length - 1; i++) {		// For each point
				g.drawRect(pixels[i].x, pixels[i + 1].y, pixels[i + 1].x - pixels[i].x, 
					adjY(0) - pixels[i + 1].y);		// Make a box
			}
			
			g.setColor(new Color(0, 0, 0));
			drawXAxis(g);
			g.setColor(new Color(150, 0, 0));			// Make ticks red
			drawTicks(g);
			g.setColor(new Color(0, 0, 100));			// Make labels blue
			labelTicks(g);
			g.setColor(new Color(0, 0, 0));
		}
	}	
}