2017-03-06 107 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); 
}  
相關問題