简体   繁体   English

旋转后Java2D图像质量下降

[英]Java2D Image quality lost after rotation

I'm using following code for rotate my image 我正在使用以下代码旋转图像

public class Field extends Canvas implements ActionListener {

    @Override
    public void paint(Graphics grphcs) {        
        super.paint(grphcs);         
        Graphics2D g2d = (Graphics2D) grphcs;
        AffineTransform affine = new AffineTransform();
        int angle = car.getAngle();
        Image image = car.getCarImage();       
        int x = (int) car.getX();
        int y = (int) car.getY();

        affine.rotate(Math.toRadians(angle), x + image.getWidth(null) / 2, 
               y + image.getHeight(null) / 2);
        g2d.setTransform(affine); 
        g2d.drawImage(image, x, y, null);     
    }  
    ....

if angle equals, for example, 5 image quality is lost. 例如,如果角度等于5,则图像质量会丢失。

在此处输入图片说明

What's the problem? 有什么问题?

You have to use antialiasing, try this: 您必须使用抗锯齿,请尝试以下操作:

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

example: the right icon is antialiased http://i.imgur.com/yeJux.png 例如:右边的图标是抗锯齿的http://i.imgur.com/yeJux.png

The other answers are half correct: Your problem can be solved by setting a RenderingHint. 其他答案是正确的一半:可以通过设置RenderingHint解决您的问题。 But in this case, it's not antialiasing. 但是在这种情况下,它不是抗锯齿。

Try adding this before you apply your transform: 在应用变换之前,请尝试添加以下内容:

g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                     RenderingHints.VALUE_RENDER_QUALITY);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM