我有2个矩形,其坐标分别为Rectangle1(x = 100,y = 100,width = 200,height = 50)和Rectangle2(x = 100,y = 200,width = 200,height = 50)。如何为它们设置仿射变换,以便它们围绕这些矩形的中心旋转(点x = 200,y = 175)。例如,在45度:
我分别设置旋转角度
this.rotate.addListener((obs, old, fresh) -> {
Rotate groupRotate = new Rotate(rotate.get(),
this.x.getValue().doubleValue() + this.width.getValue().doubleValue() / 2 ,
this.y.getValue().doubleValue() + this.height.getValue().doubleValue() / 2);
for (VObject vObject : children ) {
vObject.getShape().getTransforms().clear();
vObject.getShape().getTransforms().add(groupRotate);
}
});
是否可以在不旋转坐标轴的情况下将旋转设置为矩形?
答案 0 :(得分:1)
编辑:关于在移动旋转的对象时保留轴的更新问题:将每个旋转的对象添加到未旋转的容器中。现在,移动此容器而不是其内容。
答案 1 :(得分:1)
如果您不想使用Google Analytics(分析)几何,则应尝试使用分组对象(下面的代码是控制器类,并且是fxml
文件的一部分)
public class Controller {
@FXML
private Group groupTwoRects;
@FXML
private Rectangle rectOne;
@FXML
private Rectangle rectTwo;
@FXML
private Button btnClick;
@FXML
public void btnClick() {
groupTwoRects.setRotate(groupTwoRects.getRotate() + 45.0);
System.out.println(rectOne.getRotationAxis());
System.out.println(rectTwo.getRotationAxis());
}
}
<AnchorPane prefHeight="600.0" prefWidth="600.0">
<children>
<Button fx:id="btnClick" layoutX="14.0" layoutY="161.0" mnemonicParsing="false" onAction="#btnClick" text="Click" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" />
<Group fx:id="groupTwoRects" layoutX="62.0" layoutY="76.0" rotate="-53.1">
<children>
<Rectangle fx:id="rectOne" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="53.0" stroke="BLACK" strokeType="INSIDE" width="179.0" />
<Rectangle fx:id="rectTwo" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="53.0" layoutY="95.0" stroke="BLACK" strokeType="INSIDE" width="179.0" />
</children>
</Group>
</children>
</AnchorPane>