Java-在具有静态尺寸的两个对象之间绘制箭头

Java-在具有静态尺寸的两个对象之间绘制箭头,java,graphics,Java,Graphics,我被以下问题困住了; 我有一个矩形(50x40px,比如说,位置: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 p

我被以下问题困住了; 我有一个矩形(50x40px,比如说,位置: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);
}
这个代码显然只连接了RCT和圆圈的特定点(在这幅图中,我连接了中间的点)。你知道如何实现这样的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);

最简单的方法是设置剪裁。如果将圆和矩形添加到剪裁中,它将不会在其上绘制


但这并不能解决问题,也不能画出箭头。要解决此问题,需要使用Shape.getBounds(),计算出矩形的边界,然后计算与圆的角度,并使用三角学在矩形上找到正确的点

最简单的方法是设置剪裁。如果将圆和矩形添加到剪裁中,它将不会在其上绘制

但这并不能解决问题,也不能画出箭头。要解决此问题,需要使用Shape.getBounds(),计算出矩形的边界,然后计算与圆的角度,并使用三角学在矩形上找到正确的点