Image Quality Gets Ruined In Java Graphics2D Rotate

China☆狼群 提交于 2019-12-19 10:16:27

问题


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.

Here's my rotate method:

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?


回答1:


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.




回答2:


I know this is a pretty old question but for anyone looking to do something similar try putting the following code before your g.drawImage

g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

That should help to make the image look a better.



来源:https://stackoverflow.com/questions/11159549/image-quality-gets-ruined-in-java-graphics2d-rotate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!