JavaFX - 減法運算



顧名思義,減法運算將從另一個集合中減去一個集合的元素。大多數人通常會將減法運算與交集運算混淆,這兩個運算在操作方面完全不同。交集運算檢索兩個集合之間的公共元素,而減法運算則找到兩個集合之間的公共元素,並將其從第一個集合中移除。如果第二個集合中存在第一個集合中不存在的元素,則忽略它們。

與其他運算一樣,減法運算也應用於計算機程式設計中。在一些程式語言中,它作為差分運算子提供;但在 JavaFX 中,此運算可用於 2D 形狀。

JavaFX 中的減法運算

在 JavaFX 中,減法運算處理兩個或多個 2D 形狀覆蓋的區域。它從第一個形狀的區域中消除第二個形狀的區域。如果這兩個形狀的區域完全互斥,則第一個形狀的區域將作為結果保留。從技術上講,此運算將兩個或多個形狀作為輸入。然後,它返回第一個形狀的區域,排除第二個形狀重疊的區域,如下所示。

Subtraction Operation

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

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

以下是減法運算的示例。在這裡,我們繪製兩個圓形並對其執行減法運算。

將此程式碼儲存在名為 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 --module-path %PATH_TO_FX% --add-modules javafx.controls SubtractionExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls SubtractionExample

輸出

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

Subtraction Operation Output

示例

現在,讓我們嘗試對兩個橢圓執行減法運算,我們將從第一個橢圓中減去第二個橢圓的區域。將此檔案儲存在名為 EllipseSubtractionOperation.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 EllipseSubtractionOperation 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.subtract(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("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 --module-path %PATH_TO_FX% --add-modules javafx.controls EllipseSubtractionOperation.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls EllipseSubtractionOperation

輸出

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

Subtraction Operation Ellipse
廣告