import java.awt.*; import java.applet.*; import java.util.*; import java.awt.event.*; import java.lang.String; import java.lang.*; public class PlotPoints extends Applet { Graphics offScreenG; Image offScreenImg, backImg; Font small = new Font("Times New Roman", Font.BOLD, 20); Font medium = new Font("Times New Roman", Font.BOLD, 35); Font big = new Font("Times New Roman", Font.BOLD, 50); int X, Y; float multiplier; float limit; boolean plot, showLine, slopeDefined, inError; int numPointsOutOfRange; float yIntercept, slope; // Regression Line: y = yIntercept + slope*x float xIntercept; float drawLineXPoint1, drawLineYPoint1, drawLineXPoint2, drawLineYPoint2; Button OneByOne, TenByTen, FiftyByFifty, HundByHund; Button PlotPoints; Button Reset; Button Regression; TextField[] xs = new TextField[10]; TextField[] ys = new TextField[10]; String[] pointNames = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}; boolean[] pointNotInRange = new boolean[10]; String whichGrid = "back10by10.gif"; public void init() { backImg = getImage(getCodeBase(), whichGrid); offScreenImg = createImage(this.getSize().width, this.getSize().height); offScreenG = offScreenImg.getGraphics(); repaint(); setLayout(null); OneByOne = new Button(); OneByOne.setLabel("1 by 1"); OneByOne.setBounds(8, 325, 66, 25); add(OneByOne); TenByTen = new Button(); TenByTen.setLabel("10 by 10"); TenByTen.setBounds(77, 325, 66, 25); add(TenByTen); FiftyByFifty = new Button(); FiftyByFifty.setLabel("50 by 50"); FiftyByFifty.setBounds(146, 325, 66, 25); add(FiftyByFifty); HundByHund = new Button(); HundByHund.setLabel("100 by 100"); HundByHund.setBounds(215, 325, 66, 25); add(HundByHund); PlotPoints = new Button(); PlotPoints.setLabel("Plot My Points"); PlotPoints.setBounds(285, 2, 120, 20); add(PlotPoints); Regression = new Button(); Regression.setLabel("Show Line of Best Fit"); Regression.setBounds(410, 2, 130, 20); add(Regression); Reset = new Button(); Reset.setLabel("Reset"); Reset.setBounds(545, 2, 50, 20); add(Reset); for (int i = 0; i < 10; i++) { xs[i] = new TextField(3); xs[i].setBounds(123, 77 + 22*i, 33, 19); add(xs[i]); ys[i] = new TextField(0); ys[i].setBounds(167, 77 + 22*i, 36, 19); add(ys[i]); pointNotInRange[i] = false; } multiplier = 30; limit = 10; } public void paint (Graphics g){ backImg = getImage(getCodeBase(), whichGrid); offScreenG.drawImage(backImg,0,0,this); g.setFont(small); g.setColor(new Color(67, 113, 54)); g.drawImage(offScreenImg,0,0,this); numPointsOutOfRange = 0; inError = false; for (int i = 0; i < 10; i++) pointNotInRange[i] = false; if (plot) { for (int i = 0; i < 10; i++) { if ( (!xs[i].getText().equals("") && ys[i].getText().equals("") ) || (xs[i].getText().equals("") && !ys[i].getText().equals("") ) ) { pointNotInRange[i] = true; numPointsOutOfRange++; inError = true; } if (!xs[i].getText().equals("") && !ys[i].getText().equals("")) { float xVal, yVal; xVal = Float.parseFloat(xs[i].getText()); yVal = Float.parseFloat(ys[i].getText()); if (xVal >= 0 && xVal <= limit && yVal >= 0 && yVal <= limit) { int xPixels = 287 + (int)(xVal*multiplier); int yPixels = 327 - (int)(yVal*multiplier); g.fillRect(xPixels, yPixels, 7, 7); pointNotInRange[i] = false; } else { pointNotInRange[i] = true; numPointsOutOfRange++; inError = true; } } } if (numPointsOutOfRange > 0) { g.setColor(new Color(255, 156, 0)); g.fillRect(291, 120, 299, 10); g.fillRect(291, 230, 299, 10); g.setColor(new Color(255, 255, 255)); g.fillRect(291, 130, 299, 100); g.setColor(new Color(33, 39, 162)); int printErrorXVal = 300; if (numPointsOutOfRange == 1) { for (int i = 0; i < 10; i++) if (pointNotInRange[i]) { g.drawString(pointNames[i] + " is out of range.", 300, 175); g.drawString("Adjust this value in your chart.", 300, 195); } } else { for (int i = 0; i < 10; i++) if (pointNotInRange[i]) { if (numPointsOutOfRange != 1) { if (numPointsOutOfRange != 2) g.drawString(pointNames[i] + ", ", printErrorXVal, 165); else g.drawString(pointNames[i] + " and ", printErrorXVal, 165); printErrorXVal += 25; numPointsOutOfRange--; } else { g.drawString(pointNames[i], printErrorXVal + 35, 165); g.drawString("are out of range.", 300, 185); g.drawString("Adjust these values in your chart.", 300, 205); i = 10; } } } } } if (showLine) { DetermineRegressionLine(); g.drawLine(290 + (int)(drawLineXPoint1*multiplier) + 1, 330 - (int)(drawLineYPoint1*multiplier), 290 + (int)(drawLineXPoint2*multiplier) + 1, 330 - (int)(drawLineYPoint2*multiplier)); g.drawLine(290 + (int)(drawLineXPoint1*multiplier), 330 - (int)(drawLineYPoint1*multiplier), 290 + (int)(drawLineXPoint2*multiplier), 330 - (int)(drawLineYPoint2*multiplier)); g.drawLine(290 + (int)(drawLineXPoint1*multiplier), 330 - (int)(drawLineYPoint1*multiplier) + 1, 290 + (int)(drawLineXPoint2*multiplier), 330 - (int)(drawLineYPoint2*multiplier) + 1); } } public void update (Graphics g) { paint(g); } public void DetermineRegressionLine(){ slopeDefined = false; int pointCount = 0; float[] xVals = new float[10]; float[] yVals = new float[10]; float sumXs = 0, sumYs = 0; float meanXs = 0, meanYs = 0; float sumDiffXsTimesdiffYs = 0; float sumDiffXsSquared = 0; float xValWhenYIsLimit, yValWhenXIsLimit; for (int i = 0; i < 10; i++) if (!xs[i].getText().equals("") && !ys[i].getText().equals("")) { xVals[i] = Float.parseFloat(xs[i].getText()); yVals[i] = Float.parseFloat(ys[i].getText()); if (xVals[i] >= 0 && xVals[i] <= limit && yVals[i] >= 0 && yVals[i] <= limit) { pointCount++; sumXs += xVals[i]; sumYs += yVals[i]; } } meanXs = sumXs/pointCount; meanYs = sumYs/pointCount; for (int i = 0; i < pointCount; i++) { if (xVals[i] >= 0 && xVals[i] <= limit && yVals[i] >= 0 && yVals[i] <= limit) { sumXs += xVals[i]; sumYs += yVals[i]; } sumDiffXsTimesdiffYs += (xVals[i] - meanXs)*(yVals[i] - meanYs); sumDiffXsSquared += (xVals[i] - meanXs)*(xVals[i] - meanXs); } if (sumDiffXsSquared != 0) { slopeDefined = true; slope = sumDiffXsTimesdiffYs/sumDiffXsSquared; } yIntercept = meanYs - slope*meanXs; xIntercept = -1*yIntercept/slope; yValWhenXIsLimit = slope*limit + yIntercept; xValWhenYIsLimit = (yIntercept - limit)/(-1*slope); if (slopeDefined) { if (slope > 0) { if (xIntercept >= 0 && xIntercept <= limit) { drawLineXPoint1 = xIntercept; drawLineYPoint1 = 0; if (xValWhenYIsLimit >= 0 && xValWhenYIsLimit <= limit) { drawLineXPoint2 = xValWhenYIsLimit; drawLineYPoint2 = limit; } else { drawLineXPoint2 = limit; drawLineYPoint2 = yValWhenXIsLimit; } } else { drawLineXPoint1 = 0; drawLineYPoint1 = yIntercept; if (xValWhenYIsLimit >= 0 && xValWhenYIsLimit <= limit) { drawLineXPoint2 = xValWhenYIsLimit; drawLineYPoint2 = limit; } else { drawLineXPoint2 = limit; drawLineYPoint2 = yValWhenXIsLimit; } } } else if (slope < 0) { if (yIntercept >= 0 && yIntercept <= limit) { drawLineXPoint1 = 0; drawLineYPoint1 = yIntercept; if (xIntercept >= 0 && xIntercept <= limit) { drawLineXPoint2 = xIntercept; drawLineYPoint2 = 0; } else { drawLineXPoint2 = limit; drawLineYPoint2 = yValWhenXIsLimit; } } else { drawLineXPoint1 = xValWhenYIsLimit; drawLineYPoint1 = limit; if (xIntercept >= 0 && xIntercept <= limit) { drawLineXPoint2 = xIntercept; drawLineYPoint2 = 0; } else { drawLineXPoint2 = limit; drawLineYPoint2 = yValWhenXIsLimit; } } } else // slope = 0 { drawLineXPoint1 = 0; drawLineXPoint2 = limit; drawLineYPoint1 = drawLineYPoint2 = yIntercept; } } else { drawLineXPoint1 = drawLineXPoint2 = meanXs; drawLineYPoint1 = 0; drawLineYPoint2 = limit; } System.out.println("YIntercept: " + yIntercept); System.out.println("XIntercept: " + xIntercept); System.out.println("X Value When Y is " + limit + ": " + xValWhenYIsLimit); System.out.println("Y Value When X is " + limit + ": " + yValWhenXIsLimit); System.out.println("Slope: " + slope); } public void Reset(){ for (int i = 0; i < 10; i++) { xs[i].setText(""); ys[i].setText(""); } Regression.setLabel("Show Line of Best Fit"); multiplier = 30; limit = 10; plot = showLine = slopeDefined = inError = false; numPointsOutOfRange = 0; yIntercept = slope = xIntercept; repaint(); } public boolean action(Event evt, Object arg) { if (arg.equals("1 by 1")) { whichGrid = "back1by1.gif"; multiplier = 300; limit = 1; } if (arg.equals("10 by 10")) { whichGrid = "back10by10.gif"; multiplier = 30; limit = 10; } if (arg.equals("50 by 50")) { whichGrid = "back50by50.gif"; multiplier = 6; limit = 50; } if (arg.equals("100 by 100")) { whichGrid = "back100by100.gif"; multiplier = 3; limit = 100; } if (arg.equals("Reset")) Reset(); if (arg.equals("Plot My Points")) plot = true; if (arg.equals("Show Line of Best Fit")) { if (!inError) { showLine = true; Regression.setLabel("Hide Line of Best Fit"); } } if (arg.equals("Hide Line of Best Fit")) { showLine = false; Regression.setLabel("Show Line of Best Fit"); } repaint(); return true; } }