Javafx-rotation-transformation

提供:Dev Guides
移動先:案内検索

JavaFX-回転変換

回転では、オブジェクトを原点から特定の角度*θ(シータ)で回転させます。 次の図から、*点P(X、Y)*は、原点からの距離が *r の水平X座標からの角度φ*にあることがわかります。

回転

以下は、JavaFXでの回転変換を示すプログラムです。 ここでは、同じ場所に2つの長方形のノードを作成します。サイズは同じですが、色が異なります(BlurywoodとBlue)。 Blurywood色の長方形に回転変換も適用しています。

このコードを 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);
   }
}

次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。

javac RotationExample.java
java RotationExample

実行すると、上記のプログラムは以下に示すようにjavaFxウィンドウを生成します。

回転変換