2012-05-02 6 views
1

私は次の問題に固執しています。 私は長方形(50x40 pxは位置:x1、y1と言うことができます)とcirle(半径30、positin x2、y2)を持っています。今、私は明らかにRECT円の唯一の特定の点を結ぶそれらJava - 静的な大きさを持つ2つのオブジェクトの間に矢印を描く

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); 
} 

このコードの間の矢印を描きたい(この写真で私が真ん中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); 

答えて

1

最も簡単な方法は、クリッピングを設定することです。円と矩形をクリッピングに追加すると、クリッピングに描画されません。

問題を解決したり矢印を描いたりすることはありません。この問題を解決するには、Shape.getBounds()を使用して、矩形の境界を見つけ出し、サークルの角度を計算し、三角法を使用して四角形の正しい場所を見つけ出す必要があります。

関連する問題