java - java中将图像向右旋转90度

标签 java image swing rotation graphics2d

我无法将图像向右旋转 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;
}

关于java - java中将图像向右旋转90度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20959796/

相关文章:

java - 检索 JTextField 文本值

java - 市场计费演示应用程序中未使用的常量 (ACTION_CONFIRM_NOTIFICATION)?

java - 如何使用 GSON 将 map 的 map 转换为 JSON,然后再转换回来?

database - 在多个服务器上存储大量图像

java - 在 JButton 上添加和打开链接

java - 在 BorderLayout 中更改/设置 JTextField 的高度

java - AWS Lambda 函数不会反序列化有效负载

java - 掷骰子游戏作业

java - Argb分解/重组问题

linux - 挂载包含单个分区的镜像