Javafx-shearing-transformation
提供:Dev Guides
JavaFX-せん断変換
オブジェクトの形状を傾斜させる変換は、せん断変換と呼ばれます。 2つのせん断変換* X-せん断*および* Y-せん断*があります。 1つはX座標値をシフトし、もう1つはY座標値をシフトします。 ただし、どちらの場合も、1つの座標のみが座標を変更し、もう1つの座標はその値を保持します。 せん断は、*スキュー*とも呼ばれます。
例
以下は、JavaFXのせん断を示すプログラムです。 ここでは、同じ場所に、同じ寸法で、異なる色(青と透明)の2つのポリゴン(ノード)を作成しています。 また、透明ポリゴンにせん断を適用しています。
このコードを ShearingExample.java という名前のファイルに保存します。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.transform.Shear;
import javafx.stage.Stage;
public class ShearingExample extends Application {
@Override
public void start(Stage stage) {
Polygon hexagon1 = new Polygon();
//Adding coordinates to the hexagon
hexagon1.getPoints().addAll(new Double[]{
200.0, 50.0,
400.0, 50.0,
450.0, 150.0,
400.0, 250.0,
200.0, 250.0,
150.0, 150.0,
});
//Setting the fill color for the hexagon
hexagon1.setFill(Color.BLUE);
hexagon1.setStroke(Color.BLACK);
Polygon hexagon2 = new Polygon();
//Adding coordinates to the hexagon
hexagon2.getPoints().addAll(new Double[]{
200.0, 50.0,
400.0, 50.0,
450.0, 150.0,
400.0, 250.0,
200.0, 250.0,
150.0, 150.0,
});
//Setting the fill color for the hexagon
hexagon2.setFill(Color.TRANSPARENT);
hexagon2.setStroke(Color.BLACK);
//Creating shear transformation
Shear shear = new Shear();
//Setting the pivot points
shear.setPivotX(200);
shear.setPivotY(250);
//Setting the dimensions for the shear
shear.setX(0.5);
shear.setY(0.0);
//Adding the transformation to the polygon
hexagon2.getTransforms().addAll(shear);
//Creating a Group object
Group root = new Group(hexagon1, hexagon2);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Shearing 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 ShearingExample.java
java ShearingExample
実行時に、上記のプログラムは以下に示すようにJavaFXウィンドウを生成します。