Une rotation autour d'un point spécifié doit être composé de traduire les transformations et les rotations autour de l'origine comme suit:
- traduction Permet de déplacer le centre de la rotation à la origine.
- tourner autour de l'origine
- utiliser la traduction inverse de la première traduction
La troisième partie est absente de votre code.
Exemple
@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();
}
Vous pouvez également utiliser simplement un Rotate
avec un pivot spécifié pour obtenir l'effet désiré:
@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 (nouveau Affine (nouveau Rotation (45, rotationCenterX, rotationCenterY))); Cela a tout résolu. –