Javaの場合画像を回転させる

画像を個別に(javaで)回転させる必要があります。今のところ、g2d.drawImage(image, affinetransform, ImageObserver )しか見つかっていません。残念ながら、私は特定の点で画像を描画する必要があり、1.画像を個別に回転させ、2.xとyを設定することができます引数を持つメソッドはありません。

質問へのコメント (1)
ソリューション

このようにすることができます。このコードは 'image' というバッファリングされた画像が存在することを前提にしています(あなたのコメントにあるように)。

// The required drawing location
int drawLocationX = 300;
int drawLocationY = 300;

// Rotation information

double rotationRequired = Math.toRadians (45);
double locationX = image.getWidth() / 2;
double locationY = image.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

// Drawing the rotated image at the required drawing locations
g2d.drawImage(op.filter(image, null), drawLocationX, drawLocationY, null);
解説 (2)

AffineTransform`のインスタンスは、連結(足し算)することができます。 したがって、 '原点への移動'、 '回転'、 '目的の位置への移動'を組み合わせたトランスフォームを持つことができる。

解説 (1)

このような複雑な描画ステートメントを使用せずにそれを行う簡単な方法:

    //Make a backup so that we can reset our graphics object after using it.
    AffineTransform backup = g2d.getTransform();
    //rx is the x coordinate for rotation, ry is the y coordinate for rotation, and angle
    //is the angle to rotate the image. If you want to rotate around the center of an image,
    //use the image's center x and y coordinates for rx and ry.
    AffineTransform a = AffineTransform.getRotateInstance(angle, rx, ry);
    //Set our Graphics2D object to the transform
    g2d.setTransform(a);
    //Draw our image like normal
    g2d.drawImage(image, x, y, null);
    //Reset our graphics object so we can draw with it again.
    g2d.setTransform(backup);
解説 (0)
public static BufferedImage rotateCw( BufferedImage img )
{
    int         width  = img.getWidth();
    int         height = img.getHeight();
    BufferedImage   newImage = new BufferedImage( height, width, img.getType() );

    for( int i=0 ; i < width ; i++ )
        for( int j=0 ; j < height ; j++ )
            newImage.setRGB( height-1-j, i, img.getRGB(i,j) );

    return newImage;
}

https://coderanch.com/t/485958/java/Rotating-buffered-image より

解説 (0)