// BombArea.java import java.awt.*; public class BombArea 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 BombArea public BombArea() { super(); minX = 0; maxX = 1; minY = 0; 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 BombArea with points public BombArea(Float[] x, Float[] y) { super(); // Set up as we would for a canvas minX = 0; maxX = 1; minY = 0; 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(); w = (int) Math.min((double) d.width, (double) d.height) - 5; h = (int) Math.min((double) d.width, (double) d.height) - 5; insetX = (int) ((float) (d.width - w) / 2f + 1); insetY = (int) ((float) (d.height - h) / 2f + 1); 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)); drawGrid(g); g.setColor(new Color(0, 0, 0)); drawPoints(g); } } public void drawGrid(Graphics g) { for (int i = 0; i <= 10; i++) { g.drawLine(adjX(.1f * i), adjY(0f), adjX(.1f * i), adjY(1f)); g.drawLine(adjX(0f), adjY(.1f * i), adjX(1f), 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); } } }