// BertBoard2.java import java.awt.*; public class BertBoard2 extends java.awt.Canvas { 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 chordStarts[]; // Points that contain rescaled data points 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 BertBoard2() { super(); minX = -1; maxX = 1; minY = -1; maxY = 1; num = 0; yCoords = new Float[1]; yCoords[0] = new Float(0); } // Set up a DartBoard with points public BertBoard2(Float[] y) { super(); // Set up as we would for a canvas minX = -1; maxX = 1; minY = -1; maxY = 1; num = y.length; // get the number of data points yCoords = new Float[num]; for(int i = 0; i < num; i++) // Copy arrays, and store range variables { 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 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); chordStarts = new Point[num]; // Initialize array for actual points to be graphed 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 x1, x2, y; // current x and y coordinates int count = 0; 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 x1 = adjX((float) Math.pow((double) 1 - Math.pow(yCoords[i].floatValue(), 2f), .5)); x2 = adjX((float) (-1 * Math.pow((double) 1 - Math.pow(yCoords[i].floatValue(), 2f), .5))); y = adjY(yCoords[i]); chordStarts[i] = new Point(x1, y); chordEnds[i] = new Point(x2, y); if (Math.abs(yCoords[i].floatValue()) >= .5) count++; } return Math.round(10000f * (float) count / (float) num) / 10000f; } // 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 < chordStarts.length; i++) { g.drawLine(chordStarts[i].x, chordStarts[i].y, chordEnds[i].x, chordEnds[i].y); } } public void drawBoundary(Graphics g) { g.drawLine(adjX(0), adjY(-.5f), adjX(0), adjY(.5f)); } }