/** * The view was resized * * @param width * @param height */ public void resize(int width, int height) { this.width = width; this.height = height; axes.resize(width, height); setData(); pickX[0] = (int) ((valueX - xMin) * axes.getXScale()) + Axes.LEFT_MARGIN; pickX[1] = pickX[0]; }
protected void createPick() { pickX = new int[2]; pickY = new int[2]; pickX[0] = 0; pickX[1] = 0; pickY[0] = height - Axes.BOTTOM_MARGIN; pickY[1] = height - (axes.getHeight() + Axes.BOTTOM_MARGIN); }
/** * Constructor * * @param n * @param color */ public Graph(int n, Color color) { valueX = -1; this.color = color; length = n; stroke = new BasicStroke(1.5f); axes = new Axes(); createLine(length); createPick(); axes.setRange(xMin, xMax, yMin, yMax); }
/** * Render the graph * * @param g2d */ public void render(Graphics2D g2d) { // changed graphics context if (g2d != oldG2d) { defaultStroke = g2d.getStroke(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); oldG2d = g2d; } // render axes axes.render(g2d); // render graph line g2d.setColor(color); g2d.setStroke(stroke); g2d.drawPolyline(lineX, lineY, numPoints); g2d.setStroke(defaultStroke); // render pick line if ((pickX[0] > Axes.LEFT_MARGIN) && (pickX[0] < (width - Axes.RIGHT_MARGIN))) { g2d.setColor(pickColor); g2d.drawPolyline(pickX, pickY, 2); } }
/** * Set the graph line data * * @param vertex * @param vertexCount * @param xMin * @param xMax * @param yMin * @param yMax */ public void setData( float[] vertex, int vertexCount, float xMin, float xMax, float yMin, float yMax) { if ((xMax - xMin) == 0) { xMax += 0.0001; } if ((yMax - yMin) == 0) { yMax += 0.0001; } this.vertex = vertex; this.vertexCount = vertexCount; this.xMin = xMin; this.yMin = yMin; this.xMax = xMax; this.yMax = yMax; axes.setRange(xMin, xMax, yMin, yMax); setData(); pickX[0] = 0; pickX[1] = 0; valueX = -1; }
/** * Get the graph coordinates at the mouse X/Y. * * @param mouseX * @param mouseY * @return */ public float[] getValueAt(int mouseX, int mouseY) { valueX = (mouseX - Axes.LEFT_MARGIN) / axes.getXScale() + xMin; double y = Double.NaN; if ((valueX >= xMin) && (vertex != null)) { for (int i = 3; i < vertex.length; i += 3) { if (valueX < vertex[i]) { double w = (valueX - vertex[i - 3]) / (vertex[i] - vertex[i - 3]); y = w * (vertex[i + 1] - vertex[i - 2]) + vertex[i - 2]; break; } } } pickX[0] = mouseX; pickX[1] = mouseX; if ((valueX > xMin) && (valueX < xMax)) { return (new float[] {(float) valueX, (float) y}); } else { return (null); } }
private void setData() { buildLine(vertex, vertexCount, xMin, yMin, axes.getXScale(), axes.getYScale()); pickY[0] = height - Axes.BOTTOM_MARGIN; pickY[1] = height - (axes.getHeight() + Axes.BOTTOM_MARGIN); }