我被困在以下问题上;我有一个矩形(可以说是 50x40 像素,位置:x1,y1)和一个圆(半径 30,位置 x2,y2)。现在我想在它们之间画一个箭头
void drawArrow(Graphics g1, int x1, int y1, int x2, int y2,) {
//x1 and y1 are coordinates of circle or rectangle
//x2 and y2 are coordinates of circle or rectangle, to this point is directed the arrow
Graphics2D g = (Graphics2D) g1.create();
double dx=x2-x1;
double dy=y2-y1;
double angle = Math.atan2(dy, dx);
int len = (int) Math.sqrt(dx*dx + dy*dy);
AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);
at.concatenate(AffineTransform.getRotateInstance(angle));
g.transform(at);
g.drawLine(0,0,len,0);
g.fillPolygon(new int[] {len, len-ARR_SIZE, len-ARR_SIZE, len},
new int[] {0, -ARR_SIZE, ARR_SIZE, 0}, 4);
}
这段代码显然只连接了 rect 和 circle 的特定点(在这张图片上,我连接了中间的点http://imageshack.us/photo/my-images/341/arrk.jpg/)。你知道如何实现这样的stg吗?(http://imageshack.us/photo/my-images/833/arr2u.jpg/)...我的想法是缩短长度并计算新坐标,但我有点挣扎。
// 我这样调用这个函数:
drawArrow(g,temp.x+radius/2,temp.y+radius/2,temp2.x+width/2,temp2.y+height/2);