Javafx-2dshapes-union-operation

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

2D形状(オブジェクト)-結合操作

この操作は、2つ以上の形状を入力として受け取り、以下に示すように、それらが結合する領域を組み合わせて返します。

ユニオン操作

  • union()*と呼ばれるメソッドを使用して、シェイプでユニオン操作を実行できます。 これは静的メソッドであるため、以下に示すようにクラス名(Shapeまたはそのサブクラス)を使用して呼び出す必要があります。
Shape shape = Shape.subtract(circle1, circle2);

以下は、結合操作の例です。 ここでは、2つの円を描画し、それらに対して和集合演算を実行しています。 このコードを unionExample.java という名前のファイルに保存します。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Shape;

public class UnionExample extends Application {
   @Override
   public void start(Stage stage) {
     //Drawing Circle1
      Circle circle1 = new Circle();

     //Setting the position of the circle
      circle1.setCenterX(250.0f);
      circle1.setCenterY(135.0f);

     //Setting the radius of the circle
      circle1.setRadius(100.0f);

     //Setting the color of the circle
      circle1.setFill(Color.DARKSLATEBLUE);

     //Drawing Circle2
      Circle circle2 = new Circle();

     //Setting the position of the circle
      circle2.setCenterX(350.0f);
      circle2.setCenterY(135.0f);

     //Setting the radius of the circle
      circle2.setRadius(100.0f);

     //Setting the color of the circle
      circle2.setFill(Color.BLUE);

     //Performing union operation on the circle
      Shape shape = Shape.union(circle1, circle2);

     //Setting the fill color to the result
      shape.setFill(Color.DARKSLATEBLUE);

     //Creating a Group object
      Group root = new Group(shape);

     //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

     //Setting title to the Stage
      stage.setTitle("Union 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 UnionExample.java
java UnionExample

実行すると、上記のプログラムは、次の出力を表示するJavaFXウィンドウを生成します-

ユニオンオペレーションサークル