圍繞指定的點的旋轉需要由來自翻譯原點周圍的轉換和旋轉如下:
- 使用平移旋轉的中心移到起源。
- 繞原點
- 使用逆向翻譯的第一翻譯
第三部分是從你的代碼失蹤。
例
@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();
}
gc.transform(新仿射(新旋轉( 45,rotationCenterX,rotationCenterY)));這解決了一切。 –