// DartBoard.java

import java.awt.*;

public class DartBoard extends java.awt.Canvas {

	Float xCoords[];						// x coordinates of data points that are passed in
	Float yCoords[];						// y coordinates
	
	int w, h;								// width and height of this component
	
	float scaleX;							// Number of pixels per data unit x
	float scaleY;							// ... and for y
	
	Point pixels[];					// Points that contain rescaled data points
	
	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 DartBoard() {
		super();
		minX = -1;
		maxX = 1;
		minY = -1;
		maxY = 1;
		num = 0;
		xCoords = new Float[1];
		yCoords = new Float[1];
		xCoords[0] = new Float(0);
		yCoords[0] = new Float(0);
	}
	
	// Set up a DartBoard with points
	public DartBoard(Float[] x, Float[] y) {
		super();								// Set up as we would for a canvas
		minX = -1;
		maxX = 1;
		minY = -1;
		maxY = 1;
		num = x.length;						// get the number of data points
		xCoords = new Float[num];			// Set up arrays for x and y coordinates of data points
		yCoords = new Float[num];
		for(int i = 0; i < num; i++)		// Copy arrays, and store range variables
		{
			xCoords[i] = x[i];
			yCoords[i] = y[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 void 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);
		
		pixels = new Point[num];			// Initialize array for actual points to be graphed
		
		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
		for (int i = 0; i < num; i++) {
			// Calculate what the coordinates of each data point should be, then add a Point object to the array
			x = adjX(xCoords[i]);
			y = adjY(yCoords[i]);
			pixels[i] = 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
			setPoints();				// When we are ready to paint, the dimensions of the component have been
			g.setColor(new Color(0, 0, 100));
			drawRings(g);
			g.setColor(new Color(0, 0, 0));
			drawPoints(g);
		}
	}
	
	public void drawRings(Graphics g) {
		for (int i = 1; i <= 10; i++) {
			g.drawOval(adjX(-.1f * i), adjY(.1f * i), adjX(.1f * i) - adjX(-.1f * i),
				adjY(-.1f * i) - adjY(.1f * i));
		}
	}
	
	public void drawPoints(Graphics g) {
		for (int i = 0; i < num; i++) {
			g.drawOval(pixels[i].x, pixels[i].y, 2, 2);
		}
	}
}