JavaFX - 並集運算



從數學角度來看,並集運算在集合論的概念中得到了根本性的應用。此運算定義為將兩個不同的集合組合成一個集合。在此,一個集合的所有元素都合併到另一個集合中,而不管是否存在重複。然後,這個概念被計算機程式設計中的各種技術所採用。

例如,在 SQL 中執行並集運算時,會將兩個或多個結果集組合成一個公共結果集。它也用作程式語言(如 C、C++、Java、Python 等)中的運算子或方法。

類似地,JavaFX 也提供對 2D 形狀的並集運算。

JavaFX 中的並集運算

JavaFX 提供了可以對 2D 形狀執行的並集運算。在此,兩個或多個形狀的區域組合在一起以形成更大更復雜的形狀。因此,並集運算將兩個或多個形狀作為輸入,並返回它們佔據的組合區域,如下所示。

Union Operation

您可以使用名為 union() 的方法對形狀執行並集運算。由於這是一個靜態方法,因此您應使用類名(Shape 或其子類)呼叫它,如下所示。

Shape shape = Shape.subtract(circle1, circle2); 

示例

以下是並集運算的示例。在此,我們繪製了兩個圓形並對其執行並集運算。將此程式碼儲存在名為 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 --module-path %PATH_TO_FX% --add-modules javafx.controls UnionExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls UnionExample

輸出

執行上述程式後,將生成一個 JavaFX 視窗,顯示以下輸出:

Union Operation Circle

示例

讓我們嘗試對除圓形之外的其他形狀(如橢圓形)執行並集運算。將此檔案儲存在名為 EllipseUnionOperation.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.Ellipse; 
import javafx.scene.shape.Shape; 
         
public class EllipseUnionOperation extends Application {  
   @Override 
   public void start(Stage stage) { 
      Ellipse ellipse1 = new Ellipse();               
      ellipse1.setCenterX(250.0f); 
      ellipse1.setCenterY(100.0f); 
      ellipse1.setRadiusX(150.0f); 
      ellipse1.setRadiusY(75.0f); 
      ellipse1.setFill(Color.BLUE);     

      Ellipse ellipse2 = new Ellipse();      
      ellipse2.setCenterX(350.0f); 
      ellipse2.setCenterY(100.0f); 
      ellipse2.setRadiusX(150.0f); 
      ellipse2.setRadiusY(75.0f);     
      ellipse2.setFill(Color.RED);  
 
      Shape shape = Shape.union(ellipse1, ellipse2); 
      
      //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 --module-path %PATH_TO_FX% --add-modules javafx.controls EllipseUnionOperation.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls EllipseUnionOperation

輸出

執行上述程式後,將生成一個 JavaFX 視窗,顯示以下輸出:

Union Operation Ellipse
廣告
© . All rights reserved.