java中将图像向右旋转90度

2024-04-14

我无法将图像向右旋转 90 度。 我需要能够在java中单独旋转图像。唯一的事情。不幸的是,我需要在特定点绘制图像,并且没有带有参数的方法:1. 单独旋转图像,2. 允许我设置 x 和 y。任何帮助表示赞赏

public class Tumbler extends GraphicsProgram{

public void run() {
    setSize(1000,1000);
    GImage original = new GImage("sunset.jpg");
    add(original, 10, 10);
    int[][] pixels = original.getPixelArray();
    int height = pixels.length;
    int width = pixels[0].length;

    // Your code starts here
    int newheight = width;
    int newwidth = height;
    int[][] newpixels = new int[newheight][newwidth];

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {               
            newpixels[j][height-1-i] = pixels[i][j];            
        }
    }


    GImage image = new GImage(newpixels);
    add(image, width+20, 10);

    // Your code ends here
    }

如果我们想要获得不错的性能,我们绝对应该使用 Graphics2D(与直接复制像素相比快约 10 倍):

public static BufferedImage rotateClockwise90(BufferedImage src) {
    int width = src.getWidth();
    int height = src.getHeight();

    BufferedImage dest = new BufferedImage(height, width, src.getType());

    Graphics2D graphics2D = dest.createGraphics();
    graphics2D.translate((height - width) / 2, (height - width) / 2);
    graphics2D.rotate(Math.PI / 2, height / 2, width / 2);
    graphics2D.drawRenderedImage(src, null);

    return dest;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java中将图像向右旋转90度 的相关文章

随机推荐