旋转图像(将其恢复到原始位置)

时间:2012-05-11 02:57:14

标签: java image swing rotation paintcomponent

使用“仿射变换”我可以轻松地旋转imageA。同样,imageA将与imageB一起移动。但是,在旋转之后,我似乎无法找到将imageA移回原位的方法。

(我已经对一些网站做了一些研究,显然最好的方法是将图像移回原来的位置,使它看起来像是从一个锚点旋转。)

到目前为止我的代码:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    AffineTransform af = new AffineTransform();
    Graphics2D g2d = (Graphics2D) g;


    af.translate(imageBx, imageBy); // moves ImageA to imageb's position
    af.rotate(Math.toRadians(angle), imageB.getHeight(this) / 2, imageB.getWidth(this) / 2);


    g2d.drawImage(imageA, af, null);
    g2d.drawImage(imageB, imageBx, imageBy, null);
  }

如果有人可以帮助我将imageA移回原来的位置(就在imageB上),那将非常有帮助!

1 个答案:

答案 0 :(得分:2)

  

我看了一遍,但code旋转整个面板;我只想在固定的旋转点上旋转一个Image

有两件事可能有助于指导您的理解:

  • 引用的example使用rotate(double theta);通过转换到原点来取代,通过转换面板的中心成功。请注意,操作在声明的顺序的明显反向中执行。您的示例(可能意味着)调用rotate(double theta, double anchorx, double anchory)。这两种效果相同,后者是前者的替代方案。

  • 这个example对比了如何转换图形上下文(g2d)或图像本身。您的示例调用drawImage(Image img, AffineTransform xform, ImageObserver obs),它将xform连接到现有的图形转换;这会影响所有后续绘图。将它们分开可能更容易。

相关问题