2

次の条件で長方形を描画するJavaのメソッドは何ですか:

  • 正方形の中心の座標
  • 垂直からの長方形の角度 (度単位)
4

2 に答える 2

5

あなたが提案する方法で長方形を描くには、 class を使用する必要がありますAffineTransform。このクラスを使用して、あらゆる方法で形状を変換できます。ローテーションを実行するには:

int x = 200;
int y = 100;
int width = 50;
int height = 30;
double theta = Math.toRadians(45);

// create rect centred on the point we want to rotate it about
Rectangle2D rect = new Rectangle2D.Double(-width/2., -height/2., width, height);

AffineTransform transform = new AffineTransform();
transform.rotate(theta);
transform.translate(x, y); 
// it's been while, you might have to perform the rotation and translate in the
// opposite order

Shape rotatedRect = transform.createTransformedShape(rect);

Graphics2D graphics = ...; // get it from whatever you're drawing to

graphics.draw(rotatedRect);
于 2012-12-21T15:16:52.567 に答える
0

最初の点については、距離の式を使用して正方形の中心の座標を計算するだけで、(int)Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));それらを 2 で割ることができます。幅と高さについてこれを行うことができます。質問の内容に基づいてより良い回答を提供できるほど、Java draw について十分な知識はありませんが、お役に立てば幸いです。

2 つ目は、ポリゴンを作成するだけですよね?

于 2012-12-21T14:51:31.917 に答える