public void paintComponent(Graphics g) { super.paintComponent(g); calcSizes(); drawAxes(g); calcMaxes(); calcTickIncrements(); drawXTicks(g); drawYTicks(g); int t = 0; int prevX = 0, prevY1 = 0, prevY2 = 0; for (DataPair pair : data) { int h = pair.getH(); int p = pair.getP(); // calculate where to put the dots int x = xVal(t, maxT); int y1 = yVal(h, maxH); int y2 = yVal(p, maxP); // draw herbivore dot g.setColor(Color.BLUE); g.fillOval(x, y1, 2 * r, 2 * r); // draw a line connecting this dot to the last if (prevX != 0) { g.drawLine(prevX + r, prevY1 + r, x + r, y1 + r); } // draw predator dot g.setColor(Color.RED); g.fillOval(x, y2, 2 * r, 2 * r); // draw a line connecting this dot to the last if (prevX != 0) { g.drawLine(prevX + r, prevY2 + r, x + r, y2 + r); } // remember these dots prevX = x; prevY1 = y1; prevY2 = y2; g.setColor(Color.BLACK); t++; } }
private void calcMaxes() { maxH = -1; maxP = -1; maxT = -1; for (DataPair pair : data) { int h = pair.getH(); int p = pair.getP(); if (h > maxH) maxH = h; if (p > maxP) maxP = p; maxT++; } }