JavaFX旋转矩形关于中心?
我试图围绕它的中心旋转一个矩形。 使用GraphicsContext即gc将旋转绘制到画布上。 这是我的绘图代码。
gc.save();    
gc.translate(center.x, center.y);
gc.rotate(this.angle);
gc.strokeRect(0,0, this.width, this.height);   
gc.restore();
这将矩形移动到其中心,然后围绕其左上角旋转矩形。 我试着减去两边的长度和宽度的一半,但这只是让它飞到了这个地方。 我吮吸数学,也许有人在这里更好,可以告诉我我做错了什么。
如果需要该信息,我还存储了矩形的所有四个点(角点)。
谢谢,乔
围绕指定点的旋转需要通过翻译原点周围的变换和旋转来组成,如下所示:
第三部分从你的代码中缺失。
例
@Override
public void start(Stage primaryStage) throws Exception {
    Canvas canvas = new Canvas(400, 400);
    double x = 50;
    double y = 100;
    double width = 100;
    double height = 200;
    GraphicsContext gc = canvas.getGraphicsContext2D();
    double rotationCenterX = (x + width) / 2;
    double rotationCenterY = (y + height) / 2;
    gc.save();
    gc.translate(rotationCenterX, rotationCenterY);
    gc.rotate(45);
    gc.translate(-rotationCenterX, -rotationCenterY);
    gc.fillRect(0, 0, width, height);
    gc.restore();
    Scene scene = new Scene(new Group(canvas));
    primaryStage.setScene(scene);
    primaryStage.show();
}
  你也可以简单地用一个Rotate与指定的支点,以达到预期的效果: 
@Override
public void start(Stage primaryStage) throws Exception {
    Canvas canvas = new Canvas(400, 400);
    double x = 50;
    double y = 100;
    double width = 100;
    double height = 200;
    GraphicsContext gc = canvas.getGraphicsContext2D();
    double rotationCenterX = (x + width) / 2;
    double rotationCenterY = (y + height) / 2;
    gc.save();
    gc.transform(new Affine(new Rotate(45, rotationCenterX, rotationCenterY)));
    gc.fillRect(0, 0, width, height);
    gc.restore();
    Scene scene = new Scene(new Group(canvas));
    primaryStage.setScene(scene);
    primaryStage.show();
}
                        链接地址: http://www.djcxy.com/p/95159.html
                        上一篇: JavaFX rotate rectangle about center?
下一篇: OpenCV via NuGet packages with Visual Studio 2015, how to configure?
