3

Java で小さなゲームを作成していますが、回転する画像があります。

ここに画像の説明を入力

下の 2 つの画像でわかるように、ゲーム内でゆっくりと回転する巨大な船がありますが、特定のポイントに達すると (独自の小さな BufferedImage により) 切断されます。

私のレンダリングコードは次のとおりです。

public void drawImageRotated(BufferedImage img, double x, double y, double scale,    double angle) {
        x -= xScroll;
        y -= yScroll;  
        BufferedImage image = new BufferedImage((int)(img.getWidth() * 1.5D), (int)(img.getHeight() * 1.5D), 2);
        Graphics2D g = (Graphics2D)image.getGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.rotate(Math.toRadians(angle), image.getWidth() / 2, image.getHeight() / 2);
        g.drawImage(img, image.getWidth() / 2 - img.getWidth() / 2, image.getHeight() / 2 - image.getHeight() / 2, null);
        g2d.drawImage(image, (int)(x-image.getWidth()*scale/2), (int)(y-image.getHeight()*scale/2), (int)(image.getWidth()*scale), (int)(image.getHeight()*scale), null);
        g.dispose();      
 }

手元の問題に戻りますが、バッファリングされた画像のサイズで補正できるように、回転中に画像の最大 x および y サイズを計算するにはどうすればよいですか?

4

4 に答える 4

1

バッファリングされた画像サイズで補正できるように、回転中に画像の最大xおよびyサイズを計算するにはどうすればよいですか?

double sin = Math.abs(Math.sin(angle));
double cos = Math.abs(Math.cos(angle));
int w = image.getWidth();
int h = image.getHeight();
int neww = (int)Math.floor(w*cos+h*sin);
int newh = (int)Math.floor(h*cos+w*sin);

上記のコードは、次の例から抜粋したものです。Rotationを使用するJava(SWING)

于 2013-02-02T20:48:31.517 に答える
1

中心を中心に回転する基本的に長方形の画像がある場合、回転中の最大の幅と高さは、画像の長方形の対角線が水平または垂直の場合になります。この対角距離は、ピタゴラス定理を使用して計算し、の幅と高さに使用できますBufferedImage

    int size = (int) Math.sqrt((img.getWidth() * img.getWidth()) + (img.getHeight() * img.getHeight()));
    BufferedImage image = new BufferedImage(size, size, 2);
    // The rest of your code as before
于 2013-02-02T20:49:55.933 に答える
0

width元の画像の幅、height元の高さangle、回転角の値をラジアンで考えてみましょう。

私の計算によると、回転した画像のサイズは次のようになります。

rotatedWidth = Math.cos(angle) * width + Math.sin(angle) * height;
rotatedHeight = Math.sin(angle) * width + Math.cos(angle) * height;

役立つかもしれないので、このスレッドも見る必要があるかもしれません。

于 2013-02-02T20:48:45.853 に答える
0

別の方法は、実際のGraphicsオブジェクトを回転させ、画像を描画し、回転を復元することです。

AffineTransform old = g2d.getTransform();
g2d.rotate(Math.toRadians(angle), x + image.getWidth() / 2, y + image.getWidth() / 2);
g2d.drawImage(image, x, y, null);
g2d.setTransform(old);
于 2013-02-02T20:36:53.477 に答える