Javafx-scaling-transformation
提供:Dev Guides
JavaFX-スケーリング変換
オブジェクトのサイズを変更するには、スケーリング変換が使用されます。 スケーリングプロセスでは、オブジェクトのサイズを拡大または縮小します。 スケーリングは、目的の結果を得るために、オブジェクトの元の座標にスケーリング係数を掛けることで実現できます。
例
以下は、JavaFXのスケーリングを示すプログラムです。 ここでは、同じ場所に同じ寸法で2つの円(ノード)を作成しますが、色は異なります(BlurywoodとBlue)。 また、青色の円にスケーリング変換を適用しています。
このコードを ScalingExample.java という名前のファイルに保存します。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
public class ScalingExample extends Application {
@Override
public void start(Stage stage) {
//Drawing Circle1
Circle circle1 = new Circle(300, 135, 50);
//Setting the color of the circle
circle1.setFill(Color.BLUE);
//Setting the stroke width of the circle
circle1.setStrokeWidth(20);
//Drawing Circle2
Circle circle2 = new Circle(300, 135, 50);
//Setting the color of the circle
circle2.setFill(Color.BURLYWOOD);
//Setting the stroke width of the circle
circle2.setStrokeWidth(20);
//Creating the scale transformation
Scale scale = new Scale();
//Setting the dimensions for the transformation
scale.setX(1.5);
scale.setY(1.5);
//Setting the pivot point for the transformation
scale.setPivotX(300);
scale.setPivotY(135);
//Adding the scale transformation to circle1
circle1.getTransforms().addAll(scale);
//Creating a Group object
Group root = new Group(circle1, circle2);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Scaling 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 ScalingExample.java
java ScalingExample
実行時に、上記のプログラムは以下に示すようにJavaFXウィンドウを生成します。