java2d 给直线画箭头

时间:2022-05-06 21:54:01

现有一条直线起点坐标为(X0, Y0), 终点坐标为(X1, Y1). 在直线终点画一个箭头的Java代码可如下,

double D = Math.abs(Point2D.distance(X1, Y1, X0, Y0));
if (D > 0.0000000001 ){
Xa = X1 + ArrowLen * ((X0 - X1) + (Y0 - Y1) / 2) / D;
Ya = Y1 + ArrowLen * ((Y0 - Y1) - (X0 - X1) / 2) / D;
Xb = X1 + ArrowLen * ((X0 - X1) - (Y0 - Y1) / 2) / D;
Yb = Y1 + ArrowLen * ((Y0 - Y1) + (X0 - X1) / 2) / D;
}

filledTrigon.moveTo((float)X1, (float)Y1);
filledTrigon.lineTo((float)Xa, (float)Ya);
filledTrigon.moveTo((float)X1, (float)Y1);
filledTrigon.lineTo((float)Xb, (float)Yb);

g.draw(filledTrigon);
 

 

原文地址:http://shamaya.blogchina.com/5741083.html