Phép toán này nhận hai hoặc nhiều hình dạng làm đầu vào. Sau đó, nó trả về diện tích của hình dạng đầu tiên trừ đi diện tích chồng lấn của hình thứ hai như hình dưới.

Phương thức tĩnh subtract() của lớp javafx.scene.shape.Shape chấp nhận hai đối tượng Shape và trả về kết quả của phép trừ hai đối tượng cho trước.
Ví dụ
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 JavaFXSubtractExample extends Application {
public void start(Stage stage) {
// Vẽ circle1
Circle circle1 = new Circle();
circle1.setCenterX(230.0f);
circle1.setCenterY(100.0f);
circle1.setRadius(75.0f);
circle1.setFill(Color.DARKRED);
// Vẽ Circle2
Circle circle2 = new Circle();
circle2.setCenterX(280.0f);
circle2.setCenterY(170.0f);
circle2.setRadius(75.0f);
circle2.setFill(Color.DARKRED);
// Vẽ Circle3
Circle circle3 = new Circle();
circle3.setCenterX(330.0f);
circle3.setCenterY(100.0f);
circle3.setRadius(75.0f);
circle3.setFill(Color.DARKRED);
// Phép Subtract
Shape subtract = Shape.subtract(circle1, circle2);
subtract = Shape.intersect(subtract, circle3);
subtract.setFill(Color.RED);
// Thiết lập Stage
Group root = new Group(circle1, circle2, circle3, subtract);
Scene scene = new Scene(root, 595, 300);
stage.setTitle("Subtract Operation");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
Kết quả
