翻转和旋转图像。仿射变换不';不能在java中正常工作

翻转和旋转图像。仿射变换不';不能在java中正常工作,java,swing,java-2d,flip,affinetransform,Java,Swing,Java 2d,Flip,Affinetransform,嘿,我实现了旋转和翻转图像的代码 左、右、上下旋转都有效 翻转水平,垂直的作品 但是他们不在一起工作 当我翻转图像然后旋转时,它消失了 但是 当我翻转并翻转图像时(就像翻转之前一样),我可以正常旋转 我试图理解什么是错误的,我认为问题在于变换或缩放 你知道如何修复这个代码吗 /** * Paint the icons of this compound icon at the specified location * * @param c * The comp

嘿,我实现了旋转和翻转图像的代码

  • 左、右、上下旋转都有效
  • 翻转水平,垂直的作品
  • 但是他们不在一起工作
当我翻转图像然后旋转时,它消失了

但是

当我翻转并翻转图像时(就像翻转之前一样),我可以正常旋转


我试图理解什么是错误的,我认为问题在于变换或缩放

你知道如何修复这个代码吗

/**
 * Paint the icons of this compound icon at the specified location
 * 
 * @param c
 *            The component on which the icon is painted
 * @param g
 *            the graphics context
 * @param x
 *            the X coordinate of the icon's top-left corner
 * @param y
 *            the Y coordinate of the icon's top-left corner
 */
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2 = (Graphics2D) g.create();

    AffineTransform af = g2.getTransform();
    int cWidth = icon.getIconWidth() / 2;
    int cHeight = icon.getIconHeight() / 2;

    int xAdjustment = (icon.getIconWidth() % 2) == 0 ? 0 : -1;
    int yAdjustment = (icon.getIconHeight() % 2) == 0 ? 0 : -1;

    if (rotate == Rotate.DOWN) {

        g2.translate(x + cHeight, y + cWidth);
        g2.rotate(Math.toRadians(90));
        icon.paintIcon(c, g2, -cWidth, yAdjustment - cHeight);

    } else if (rotate == Rotate.UP) {

        g2.translate(x + cHeight, y + cWidth);
        g2.rotate(Math.toRadians(-90));
        icon.paintIcon(c, g2, xAdjustment - cWidth, -cHeight);

    } else if (rotate == Rotate.UPSIDE_DOWN) {

        g2.translate(x + cWidth, y + cHeight);
        g2.rotate(Math.toRadians(180));
        icon.paintIcon(c, g2, xAdjustment - cWidth, yAdjustment - cHeight);

    } else if (rotate == Rotate.VERTICAL) { 

         g2.translate(0, getIconHeight());  
         g2.scale(1, -1); 

         icon.paintIcon(c, g2, x, y); 
         vert = !vert; //boolean flag

    } else if (rotate == Rotate.HORIZONTAL) {

        g2.translate(getIconWidth(), 0);  
        g2.scale(-1, 1);  
        icon.paintIcon(c, g2, x, y); 
        hor = !hor; //boolean flag

    } else if (rotate == Rotate.VERTICALLY_HORIZONTAL) {

        g2.translate(getIconWidth(), getIconHeight());  
        g2.scale(-1, -1);  
        icon.paintIcon(c, g2, x, y);
        hor = !hor;
        vert = !vert;

    } else if (rotate == Rotate.CENTER) {

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        AffineTransform original = g2.getTransform();
        AffineTransform at = new AffineTransform();
        at.concatenate(original);
        at.translate((getIconWidth() - icon.getIconWidth()) / 2,
                (getIconHeight() - icon.getIconHeight()) / 2);
        at.rotate(Math.toRadians(angle), x + cWidth, y + cHeight);
        g2.setTransform(at);
        icon.paintIcon(c, g2, x, y);
        g2.setTransform(original);

    }
}

如果你想让它们一起工作,你必须保存你用变换制作的图像。这里有一个例子

图像源=。。。;
图像转换拷贝=。。。;
仿射变换在=。。。;
transformedCopy.getGraphics().setTransform(at).drawImage(orig);
//transformedCopy现在将拥有已转换图像的副本

图标之后。paintIcon
需要恢复
g2
的原始状态,执行反向操作。可能,但当布尔标志vert设置为true时,我尝试执行此反向操作:if(vert){g2.translate(0,getIconHeight());g2.scale(1,-1);}有人知道如何从数学上求逆吗?
g2.scale(1,-1);g2.translate(0,-getIconHeight())当有垂直翻转时,我设置布尔标志“vert”,在if块中向左(向上)和向下(向右)旋转,我在正常平移和旋转之前进行反转,它不起作用是的,但是,我使用ImageIcon,所以我必须保存:-ImageIcon-Graphics2D-Graphics-Icon?ImageIcon.setImage(transformedCopy);