// BertBoard2.java

import java.awt.*;

public class BertBoard3 extends java.awt.Canvas {

	Float angle[];
	
	int w, h;								// width and height of this component
	
	float scaleX;							// Number of pixels per data unit x
	float scaleY;							// ... and for y
	
	Point chordEnds[];
	
	float minX, maxX, minY, maxY;		// Range variables for graph
	int num;						// Number of data points
	
	int insetX, insetY;						// Inset around edge of component
	
	// Set up a DartBoard
	public BertBoard3() {
		super();
		minX = -1;
		maxX = 1;
		minY = -1;
		maxY = 1;
		num = 0;
		angle = new Float[1];
		angle[0] = new Float(0);
	}
	
	// Set up a DartBoard with points
	public BertBoard3(Float[] a) {
		super();								// Set up as we would for a canvas
		minX = -1;
		maxX = 1;
		minY = -1;
		maxY = 1;
		num = a.length;						// get the number of data points
		angle = new Float[num];
		for(int i = 0; i < num; i++)		// Copy arrays, and store range variables
		{
			angle[i] = a[i];
		}
	}
	
	public int adjX(Float oldX) {
		return adjX(oldX.floatValue());
	}
	
	public int adjY(Float oldY) {
		return adjY(oldY.floatValue());
	}
	
	public int adjX(float oldX) {
		return (int) ((float) (oldX - minX) * scaleX) + insetX;
	}
	
	public int adjY(float oldY) {
		return (int) ((float) (maxY - oldY) * scaleY) + insetY;
	}
	
	// Determines size of this object, and scales the coordinates accordingly.
	public float setPoints() {
		Dimension d = size();  //getSize() in 1.1.1
		w = (int) Math.min((double) d.width, (double) d.height);				
		h = (int) Math.min((double) d.width, (double) d.height);
		insetX = (int) ((float) (d.width - w) / 2f);
		insetY = (int) ((float) (d.height - h) / 2f);
		
		chordEnds = new Point[num];
		
		if (maxX - minX != 0)
			scaleX = (float) w / (maxX - minX);	// Total width / range
													// = number of pixels for each point
		if (maxY - minY != 0)						// Simalarly for y scale
			scaleY = (float) h / (maxY - minY);
			
		int x, y;									// current x and y coordinates
		int count = 0;
		for (int i = 0; i < num; i++) {
			x = adjX((float) Math.cos((double) angle[i].floatValue()));
			y = adjY((float) Math.sin((double) angle[i].floatValue()));
			chordEnds[i] = new Point(x, y);
			if (angle[i].floatValue() >= (2f/3f) * (float) Math.PI && 
					angle[i].floatValue() <= (4f/3f) * (float) Math.PI)
				count++;
		}
		return Math.round(10000f * (float) count / (float) num) / 10000f;
	}
	
	
	/* public Point coordsOf(Point start, float angle, float distance) {
		int x, y;
		x = start.x + (int) (Math.cos(angle) * distance);
		y = start.y - (int) (Math.sin(angle) * distance);
		return new Point(x, y);
	} */
	
	// Paint method, which should be redefined by subclasses to actually show the data points
	public void paint(Graphics g) {
		if (num > 0) { 					// Make sure we have at least one point
			g.setColor(new Color(0, 0, 100));
			drawRing(g);
			g.setColor(new Color(0, 0, 0));
			drawChords(g);
			g.setColor(new Color(100, 0, 0));
			drawBoundary(g);
		}
	}
	
	public void drawRing(Graphics g) {
		for (int i = 1; i <= 10; i++) {
			g.drawOval(adjX(-1), adjY(1), adjX(1) - adjX(-1),
				adjY(-1) - adjY(1));
		}
	}
	
	public void drawChords(Graphics g) {
		for (int i = 0; i < chordEnds.length; i++) {
			g.drawLine(adjX(1), adjY(0), chordEnds[i].x, chordEnds[i].y);
		}
	}
	
	public void drawBoundary(Graphics g) {
		g.drawLine(adjX((float) Math.cos((2f/3f) * Math.PI)), adjY((float) Math.sin((2f/3f) * Math.PI)),
			adjX((float) Math.cos((4f/3f) * Math.PI)), adjY((float) Math.sin((4f/3f) * Math.PI)));
	}
}