2017-03-06 71 views
0

假设画布X(x0,y0)和Y(x1,y1)上有两个点。我如何绘制X-> Y箭头?如果x_i或y_i坐标相等,我没有问题,但我不知道该怎么做,如果必须旋转箭头的头部。有任何想法吗?如何在JavaFX中的两点之间绘制箭头?

+0

您为解决您的问题所做的任何努力,或者您期望得到正确的解决方案?创建[mcve],描述并显示你的问题。 – MBec

+0

[如何在Java中绘制指向箭头线?](http://stackoverflow.com/questions/2027613/how-to-draw-a-directed-arrow-line-in-java) –

+0

@ cszoltan422 ,试试这个'graphics_context.strokeLine(x0,y0,x1,y1); ' –

回答

1

终于有了解决办法!由于此应用程序显示图形,我称这些点为节点。 所以:

private void drawArrow(GraphicsContext gc, double node1X, double node1Y, double node2X, double node2Y) { 
    double arrowAngle = Math.toRadians(45.0); 
    double arrowLength = 10.0; 
    double dx = node1X - node2X; 
    double dy = node1Y - node2Y; 
    double angle = Math.atan2(dy, dx); 
    double x1 = Math.cos(angle + arrowAngle) * arrowLength + node2X; 
    double y1 = Math.sin(angle + arrowAngle) * arrowLength + node2Y; 

    double x2 = Math.cos(angle - arrowAngle) * arrowLength + node2X; 
    double y2 = Math.sin(angle - arrowAngle) * arrowLength + node2Y; 
    gc.strokeLine(node2X, node2Y, x1, y1); 
    gc.strokeLine(node2X, node2Y, x2, y2); 
}  
相关问题