a.setLocation(xa, ya); if (b2.equals(new Point2D.Double())) { transform.transformA2B(a, b1); b2.setLocation(b1); } else { b1.setLocation(b2); transform.transformA2B(a, b2); if (!Double.isNaN(ya)) { a.setLocation(xa, ya); transform.transformA2B(a, b1); Rectangle2D.Double r = new Rectangle2D.Double(b1.getX() - 1.5, b1.getY() - 1.5,
private void drawValueIndicator(Graphics2D g2d) { Diagram.RectTransform transform = diagram.getTransform(); Point2D a = transform.transformB2A(dragPoint, null); double x = a.getX(); if (x < selectedGraph.getXMin()) { x = selectedGraph.getXMin(); } if (x > selectedGraph.getXMax()) { x = selectedGraph.getXMax(); } final Stroke oldStroke = g2d.getStroke(); final Color oldColor = g2d.getColor(); double y = getY(selectedGraph, x); Point2D b = transform.transformA2B(new Point2D.Double(x, y), null); g2d.setStroke(new BasicStroke(1.0f)); g2d.setColor(diagram.getForegroundColor()); Ellipse2D.Double marker = new Ellipse2D.Double(b.getX() - 4.0, b.getY() - 4.0, 8.0, 8.0); g2d.draw(marker); g2d.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{6, 6}, 12)); g2d.setColor(diagram.getForegroundColor()); final Rectangle graphArea = diagram.getGraphArea(); g2d.draw(new Line2D.Double(b.getX(), graphArea.y + graphArea.height, b.getX(), b.getY())); g2d.draw(new Line2D.Double(graphArea.x, b.getY(), b.getX(), b.getY())); DecimalFormat decimalFormat = new DecimalFormat("0.#####E0"); String text = selectedGraph.getYName() + ": x = " + decimalFormat.format(x) + ", y = " + decimalFormat.format(y); g2d.setStroke(oldStroke); g2d.setColor(oldColor); drawTextBox(g2d, text, graphArea.x + 6, graphArea.y + 6 + 16, new Color(255, 255, 255, 128)); }
private void validate(Graphics2D g2d, int x, int y, int width, int height) { fontMetrics = g2d.getFontMetrics(); xTickTexts = xAxis.createTickmarkTexts(); yTickTexts = yAxis.createTickmarkTexts(); // define y-Axis _values final int fontAscent = fontMetrics.getAscent(); maxYTickTextWidth = 0; for (String yTickText : yTickTexts) { int sw = fontMetrics.stringWidth(yTickText); maxYTickTextWidth = Math.max(maxYTickTextWidth, sw); } final int widthMaxX = fontMetrics.stringWidth(xTickTexts[xTickTexts.length - 1]); int x1 = textGap + fontAscent + textGap + maxYTickTextWidth + textGap + majorTickLength; int y1 = textGap + fontAscent / 2; int x2 = x + width - (textGap + widthMaxX / 2); int y2 = y + height - (textGap + fontAscent + textGap + fontAscent + textGap + majorTickLength); final int w = x2 - x1 + 1; final int h = y2 - y1 + 1; graphArea = new Rectangle(x1, y1, w, h); transform = null; if (w > 0 && h > 0) { transform = new RectTransform(new Range(xAxis.getMinValue(), xAxis.getMaxValue()), new Range(yAxis.getMinValue(), yAxis.getMaxValue()), new Range(graphArea.x, graphArea.x + graphArea.width), new Range(graphArea.y + graphArea.height, graphArea.y)); } setValid(w > 0 && h > 0); }
public DiagramGraph getClosestGraph(int x, int y) { double minDist = Double.MAX_VALUE; Point2D.Double a = new Point2D.Double(); Point2D.Double b1 = new Point2D.Double(); Point2D.Double b2 = new Point2D.Double(); DiagramGraph closestGraph = null; for (DiagramGraph graph : getGraphs()) { double minDistGraph = Double.MAX_VALUE; int n = graph.getNumValues(); for (int i = 0; i < n; i++) { a.setLocation(graph.getXValueAt(i), graph.getYValueAt(i)); b1.setLocation(b2); transform.transformA2B(a, b2); if (i > 0) { Line2D.Double segment = new Line2D.Double(b1, b2); double v = segment.ptSegDist(x, y); if (v < minDistGraph) { minDistGraph = v; } } } if (minDistGraph < minDist) { minDist = minDistGraph; closestGraph = graph; } } return closestGraph; }
public void testTransform() { Diagram.RectTransform rectTransform = new Diagram.RectTransform(new Range(0, 10), new Range(-1, +1), new Range(100, 200), new Range(100, 0)); Point2D a, b; a = new Point2D.Double(5, 0); b = rectTransform.transformA2B(a, null); assertEquals(new Point2D.Double(150.0, 50.0), b); assertEquals(a, rectTransform.transformB2A(b, null)); a = new Point2D.Double(7.5, -0.25); b = rectTransform.transformA2B(a, null); assertEquals(new Point2D.Double(175.0, 62.5), b); assertEquals(a, rectTransform.transformB2A(b, null)); }