We can use the static Path methods, intersect, subtract and union, to perform different operations with 2 shapes.
Here they are applied to 3 circle and 3 rectangles, with overlap. The intersect will result in the lowest area, while union with the most.
package ex17;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
public class Ex17 extends Application {
@Override
public void start(Stage primaryStage) {
Group root = new Group();
double w = 50, rad = 25, x = 100, y = 100;
Circle circ1 = new Circle(x, y, rad);
Circle circ2 = new Circle(2*x, y, rad);
Circle circ3 = new Circle(3*x, y, rad);
Rectangle rect1 = new Rectangle(x, y, w, w);
Rectangle rect2 = new Rectangle(2*x, y, w, w);
Rectangle rect3 = new Rectangle(3*x, y, w, w);
Shape intersect = Path.intersect(rect1, circ1);
intersect.setFill(Color.RED);
Shape subtract = Path.subtract(rect2, circ2);
subtract.setFill(Color.RED);
Shape union = Path.union(rect3, circ3);
union.setFill(Color.RED);
root.getChildren().addAll(intersect, subtract, union);
Scene scene = new Scene(root, 400, 300, Color.HONEYDEW);
primaryStage.setTitle("Example 17. Intersect, subtract, and union");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This is the output: