JavaFX - Transformation de rotation

En rotation, nous faisons pivoter l'objet selon un angle particulier θ (theta)depuis son origine. À partir de la figure suivante, nous pouvons voir que lepoint P(X, Y) est situé à angle φ à partir de la coordonnée horizontale X avec distance r de l'origine.

Exemple

Voici le programme qui montre la transformation de rotation dans JavaFX. Ici, nous créons 2 nœuds rectangulaires au même endroit, avec les mêmes dimensions mais avec des couleurs différentes (Blurywood et Blue). Nous appliquons également la transformation de rotation sur le rectangle avec la couleur Blurywood.

Enregistrez ce code dans un fichier avec le nom RotationExample.java.

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Rectangle; 
import javafx.scene.transform.Rotate; 
import javafx.stage.Stage; 
         
public class RotationExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing Rectangle1 
      Rectangle rectangle1 = new Rectangle(150, 75, 200, 150); 
      rectangle1.setFill(Color.BLUE); 
      rectangle1.setStroke(Color.BLACK);  
      
      //Drawing Rectangle2 
      Rectangle rectangle2 = new Rectangle(150, 75, 200, 150); 
      
      //Setting the color of the rectangle 
      rectangle2.setFill(Color.BURLYWOOD); 
      
      //Setting the stroke color of the rectangle 
      rectangle2.setStroke(Color.BLACK); 
       
      //creating the rotation transformation 
      Rotate rotate = new Rotate(); 
      
      //Setting the angle for the rotation 
      rotate.setAngle(20); 
      
      //Setting pivot points for the rotation 
      rotate.setPivotX(150); 
      rotate.setPivotY(225); 
        
      //Adding the transformation to rectangle2 
      rectangle2.getTransforms().addAll(rotate); 
        
      //Creating a Group object
      Group root = new Group(rectangle1, rectangle2); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Rotation transformation example"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}

Compilez et exécutez le fichier java enregistré à partir de l'invite de commande à l'aide des commandes suivantes.

javac RotationExample.java 
java RotationExample

Lors de l'exécution, le programme ci-dessus génère une fenêtre javaFx comme indiqué ci-dessous.