图像质量在 Java Graphics2D Rotate 中被破坏

2022-01-16 00:00:00 image rotation java graphics2d

我在使用 Graphics2D 旋转方法旋转图像时遇到问题.

I am experiencing an issue with rotating an Image with the Graphics2D rotate method.

这是我遇到的问题的图片:

Here's an image of the issue I'm having:

当我移动球时,图像在旋转时完全扭曲.

As I move the ball, the image gets completely distorted as it rotates.

这是我的旋转方法:

public static void rotate(BufferedImage img, Rectangle rect, int degrees) { 
    Graphics2D g = (Graphics2D) img.createGraphics();
    g.rotate(degrees, rect.x + rect.width/2, rect.y + rect.height/2);
    g.drawImage(img, rect.x, rect.y, rect.width, rect.height, null);
    g.dispose();
}

我可以做些什么来避免质量损失?

Is there anything I can do to avoid the quality loss?

推荐答案

那么当前随着球的移动,你将图像旋转几度并用新的图像覆盖原始图像?每次旋转图像时,都会添加一点点失真;经过多次旋转,扭曲变得更加复杂.

So currently as the ball moves, you rotate the image a few degrees and overwrite the original image with the new one? Each time the image is rotated, a tiny bit of distortion is added; over many rotations, the distortion is compounded.

相反,只需将原始图像保存在内存中,并存储它应该在屏幕上显示的旋转度数.然后每次渲染时,将原始图像旋转当前的度数.

Instead, just keep the original image in memory and also store the number of degrees it should appear to be rotated on screen. Then each time you render it, rotate the original image by the current number of degrees.

相关文章