Phép toán này nhận hai hoặc nhiều hình dạng làm đầu vào và trả về diện tích được chiếm bởi chúng kết hợp như hình dưới.

Phương thức tĩnh union() 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 hợp 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 JavaFXUnionExample 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 Union
Shape union = Shape.union(circle1, circle2);
union = Shape.union(union, circle3);
union.setFill(Color.RED);
// Thiết lập Stage
Group root = new Group(circle1, circle2, circle3, union);
Scene scene = new Scene(root, 595, 300);
stage.setTitle("Union Operation");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
Kết quả
