Javafx-2dshapes-subtraction-operation

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

2D形状(オブジェクト)減算操作

この操作は、入力として2つ以上の形状を取ります。 次に、以下に示すように、2番目の図形と重なる領域を除く、最初の図形の領域を返します。

減算操作

  • subtract()*という名前のメソッドを使用して、図形に対して減算操作を実行できます。 これは静的メソッドであるため、以下に示すようにクラス名(Shapeまたはそのサブクラス)を使用して呼び出す必要があります。
Shape shape = Shape.subtract(circle1, circle2);

以下は、減算演算の例です。 ここでは、2つの円を描き、それらに対して減算操作を実行しています。

このコードを SubtractionExample.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 SubtractionExample 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 subtraction operation on the circle
      Shape shape = Shape.subtract(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("Subtraction 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 SubtractionExample.java
java SubtractionExample

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

減算演算出力