0

I am required to write something similar to graphviz. I have already managed to draw the nodes so that they do not overlap and to connect their center with a quadratic Bezier curve, but I am facing the following problem with the directionality of a curved edge:

enter image description here

As you can see, the yellow little triangle is not quite nice. My code is the following:

    g.setColor(Color.RED);
    ArrowGraph arrow = graph.getArrows().get(0);    
    int x = (int)(arrow.getP1().getX() + arrow.getP2().getX()) / 2;
    int y = (int)(arrow.getP1().getY() + arrow.getP2().getY()) / 2 + 50;
    QuadCurve2D.Double curve = new QuadCurve2D.Double(arrow.getP1().getX() - 20, arrow.getP1().getY() - 20, x, y, arrow.getP2().getX() - 20, arrow.getP2().getY() - 20);         
    ((Graphics2D)g).draw(curve);            

    int[] xPoints = new int[] {(int)arrow.getP2().getX() - 20, (int)arrow.getP2().getX() - 15, (int)arrow.getP2().getX() - 25};
    int[] yPoints = new int[] {(int)arrow.getP2().getY() - 20, (int)arrow.getP2().getY() - 10, (int)arrow.getP2().getY() - 10};
    g.setColor(Color.YELLOW);
    g.fillPolygon(xPoints, yPoints, 3);

The coordinates that I know: the start and end point and the control point of the curve. The start and the end points are basically the centers of the nodes.

I would like to somehow adjust the triangle to the curve, so it's on the end of it (preferably at border of the node). Any idea how I could do that?

Eszter
  • 331
  • 1
  • 3
  • 19

1 Answers1

1

You can use FlatteningPathIterator passing your curve and get the last segment (last line on the path in fact).

The use the line to to build the arrow triangle.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • And by using the last line on the path, I can get a point on the path, according to which I can construct the triangle. This answer has been helpful, I haven't known of FlatteningPathIterator. – Eszter Jan 16 '15 at 13:15