我无法找出适用于我的应用程序中元素的正确转换顺序。该应用程序具有父/子关系的元素,每个元素都有一个转换(每个元素都在本地空间中绘制,然后转换)。java仿Affine变换正确的顺序
我想归档的是,如果我转换父母,所有的孩子也应该变形(所以如果你旋转父母,孩子应该围绕父母旋转)。
下面的代码就是这样,但问题是当我旋转父,然后想要移动孩子。它向错误的方向移动(由于其父变换)。我试图改变转换顺序,但没有运气(我知道我应该先翻译,然后转换,但随后孩子围绕自己的轴旋转 - 而不是父母)。
代码:
Element e;
AffineTransform t = new AffineTransform();
AffineTransform t3 = new AffineTransform();
for (i = 0; i < el.size(); i++)
{
e = el.get(i);
t3.setToIdentity();
t.setToIdentity();
tmp = e.getParent();
while(tmp != null)
{
t.translate(tmp.getX(), tmp.getY());
t3.rotate(Math.toRadians(tmp.getAngle()),tmp.getAnchorX(), tmp.getAnchorY());
tmp = tmp.getParent();
}
t.concatenate(t3);
t.translate(e.getX(), e.getY());
t.rotate(Math.toRadians(e.getAngle()),e.getAnchorX(), e.getAnchorY());
e.draw(g2d,t);
}
问题: - 鉴于两个元素(其他一个孩子) - 父母是旋转(40度) - 孩子随后被10px的 移动我怎么能连接变换,以便当我移动孩子时,它不会沿旋转的方向移动?
编辑: 是Torious发布(没有工作)代码:
public AffineTransform getTransformTo(Element ancestor) {
AffineTransform t = (AffineTransform)getAffineTransform().clone();
Element parent = getParent();
while (parent != null && parent != ancestor) {
t.preConcatenate(parent.getAffineTransform());
parent = parent.getParent();
}
return t;
}
public void translateInAncestorSpace(Element ancestor, Point translation) {
AffineTransform fromAncestor = getTransformTo(ancestor); // to ancestor space
try
{
fromAncestor.invert();
} catch(Exception e)
{
e.printStackTrace();
}
translation = (Point)fromAncestor.transform(translation, new Point());
Transform t1 = new Transform();
t1.translate(translation.x,translation.y);
transform(t1);
}
输出代码:
Moving by [x=1,y=1] old position [x=22,y=40]
Moved by [x=-21,y=-39] new position [x=1,y=1]
Moving by [x=2,y=2] old position [x=1,y=1]
Moved by [x=1,y=1] new position [x=2,y=2]
Moving by [x=4,y=3] old position [x=2,y=2]
Moved by [x=2,y=1] new position [x=4,y=3]
为了更快提供更好的帮助,请发布[SSCCE](http://sscce.org/)。顺便说一句 - 你有问题吗? – 2012-04-22 14:27:15