Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:旋转图像_Java_Image_Swing_Rotation_Graphics2d - Fatal编程技术网

Java:旋转图像

Java:旋转图像,java,image,swing,rotation,graphics2d,Java,Image,Swing,Rotation,Graphics2d,我需要能够单独旋转图像(在java中)。到目前为止,我只找到了g2d.drawImage(image,affinetransform,ImageObserver)。不幸的是,我需要在一个特定的点上绘制图像,并且没有一个参数为1.1和2的方法。允许我设置x和y。非常感谢您的帮助这是您可以做到的。此代码假定存在一个名为“image”的缓冲图像(如您的注释所示) AffineTransform实例可以连接(添加在一起)。因此,您可以有一个组合“移到原点”、“旋转”和“移回所需位置”的变换。公共静态缓冲

我需要能够单独旋转图像(在java中)。到目前为止,我只找到了g2d.drawImage(image,affinetransform,ImageObserver)。不幸的是,我需要在一个特定的点上绘制图像,并且没有一个参数为1.1和2的方法。允许我设置x和y。非常感谢您的帮助

这是您可以做到的。此代码假定存在一个名为“image”的缓冲图像(如您的注释所示)


AffineTransform
实例可以连接(添加在一起)。因此,您可以有一个组合“移到原点”、“旋转”和“移回所需位置”的变换。

公共静态缓冲区图像旋转cw(缓冲区图像img)
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;
}
{ int width=img.getWidth(); int height=img.getHeight(); BuffereImage newImage=新的BuffereImage(高度、宽度、img.getType()); 对于(int i=0;i

一种不使用如此复杂的绘图语句的简单方法:

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

对不起,作为一个图形初学者,所有的答案都很难理解

在做了一些修改之后,这对我来说是有效的,很容易推理

@Override
public void draw(Graphics2D g) {
    AffineTransform tr = new AffineTransform();
    // X and Y are the coordinates of the image
    tr.translate((int)getX(), (int)getY());
    tr.rotate(
            Math.toRadians(this.rotationAngle),
            img.getWidth() / 2,
            img.getHeight() / 2
    );

    // img is a BufferedImage instance
    g.drawImage(img, tr, null);
}

我想如果你想旋转一个矩形图像,这种方法不起作用,会剪切图像,但我认为你应该创建一个方形png图像并旋转它。

为什么这么复杂?转换包含旋转和平移,因此只需使用应答中的
tx
执行
g2d.drawImage(image,tx,ImageObserver)
。谢谢,但它会切断一些图像。有关如何避免切断问题的任何信息?请参阅:
@Override
public void draw(Graphics2D g) {
    AffineTransform tr = new AffineTransform();
    // X and Y are the coordinates of the image
    tr.translate((int)getX(), (int)getY());
    tr.rotate(
            Math.toRadians(this.rotationAngle),
            img.getWidth() / 2,
            img.getHeight() / 2
    );

    // img is a BufferedImage instance
    g.drawImage(img, tr, null);
}