如何用AffineTransform旋转并保持原始坐标系?

发布于 2024-10-15 21:22:15 字数 1136 浏览 1 评论 0原文

我是一个在 Java Swing 上旋转和平移形状的命令模式 平移和旋转分别工作得很好,但是当我做 60 度时。旋转然后平移,平移遵循新的旋转坐标。 这意味着如果我拖动鼠标,形状会以与鼠标移动矢量 60 度的间隙移动 有什么简单的解决办法吗?请帮忙,我在这里碰壁了

我的旋转执行方法

public void execute() {  
    System.out.println("command: rotate " + thetaDegrees );       
    Iterator iter = objects.iterator();  
    Shape shape;  
    while(iter.hasNext()){  
        shape = (Shape)iter.next();           
        mt.addMememto(shape);             
        AffineTransform t =  shape.getAffineTransform();      
        t.rotate(Math.toRadians(thetaDegrees), shape.getCenter().x, shape.getCenter().y);  
        shape.setAffineTransform(t);              
    }  
}  

我的翻译执行方法

public void execute() {  
    Iterator iter = objects.iterator();  
    Shape shape;  
    while(iter.hasNext()){  
        shape = (Shape)iter.next();  
        mt.addMememto(shape);  
        AffineTransform t = shape.getAffineTransform();  
        System.out.println("Translation x :"+x + ", Translation y :"+y);  
        t.translate(x,y);  
        shape.setAffineTransform(t);  
    }  
} 

任何帮助都可以非常感谢

I'am a command pattern to rotate and translate shapes on Java Swing
The translation and the rotation work well separatly, but when I do a 60 deg. rotation and then the translation, the translation follow the new rotated coordinate.
Which means if I drag the mouse, the shape moves with a 60 deg gap from the mouse mouvement vector
is there any easy solution ? please help, I'am hitting a wall here

My execute method for the rotation

public void execute() {  
    System.out.println("command: rotate " + thetaDegrees );       
    Iterator iter = objects.iterator();  
    Shape shape;  
    while(iter.hasNext()){  
        shape = (Shape)iter.next();           
        mt.addMememto(shape);             
        AffineTransform t =  shape.getAffineTransform();      
        t.rotate(Math.toRadians(thetaDegrees), shape.getCenter().x, shape.getCenter().y);  
        shape.setAffineTransform(t);              
    }  
}  

My execute method for the translation

public void execute() {  
    Iterator iter = objects.iterator();  
    Shape shape;  
    while(iter.hasNext()){  
        shape = (Shape)iter.next();  
        mt.addMememto(shape);  
        AffineTransform t = shape.getAffineTransform();  
        System.out.println("Translation x :"+x + ", Translation y :"+y);  
        t.translate(x,y);  
        shape.setAffineTransform(t);  
    }  
} 

Any help can be really appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹊巢 2024-10-22 21:22:15

要完成“就地”旋转(对象绕其自身轴旋转),您必须:

  1. 将对象平移到原点
  2. 应用旋转
  3. 平移回原始位置
  4. 应用所需的平移

请注意,步骤 3 和 4 可以是立即申请。

如果尝试在原点以外的位置进行旋转,则会实现“旋转”效果 - 对象看起来围绕原点旋转。

To accomplish an "in-place" rotation (where the object rotates about its own axis), you must:

  1. translate the object to the origin
  2. apply the rotation
  3. translate back to the original position
  4. apply the desired translation

Note that steps 3 and 4 could be applied at once.

If rotation is attempted at position other than the origin, a "revolving" effect is achieved - one where the object appears to be revolving about the origin.

泅渡 2024-10-22 21:22:15

您正在使用特殊的旋转函数,该函数将考虑形状的偏移,以便围绕其中心正确旋转。但是,您需要对平移函数执行类似的操作,以便考虑形状的方向。

尝试使用此方法来代替您的翻译功能:

public void execute() {  
    Iterator iter = objects.iterator();  
    Shape shape;  
    while(iter.hasNext()){  
        shape = (Shape)iter.next();  
        mt.addMememto(shape);  
        AffineTransform t = new AffineTransform();
        System.out.println("Translation x :"+x + ", Translation y :"+y);
        t.translate(x,y);
        t.concatenate(shape.getAffineTransform());
        shape.setAffineTransform(t);  
    }  
}

这将在原始坐标系中执行翻译。

You're using a special rotate function that will take account of the offset of the shape in order to properly rotate around its center. However you need to do something similar for the translate function in order to take account of the orientation of the shape.

Try this instead for your translate function:

public void execute() {  
    Iterator iter = objects.iterator();  
    Shape shape;  
    while(iter.hasNext()){  
        shape = (Shape)iter.next();  
        mt.addMememto(shape);  
        AffineTransform t = new AffineTransform();
        System.out.println("Translation x :"+x + ", Translation y :"+y);
        t.translate(x,y);
        t.concatenate(shape.getAffineTransform());
        shape.setAffineTransform(t);  
    }  
}

This executes the translation in the original coordinate system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文