解释 JavaFX 中对 2D 形状执行 Subtract(减)操作
此操作将两个或更多形状作为输入。然后,它返回第一个形状的面积,不包括被第二个形状重叠的面积,如下所示。
javafx.scene.shape.Shape 类的subtract()(静态)方法接受两个 Shape 对象并返回给定对象减操作的结果。
示例
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) { //Drawing circle1 Circle circle1 = new Circle(); circle1.setCenterX(230.0f); circle1.setCenterY(100.0f); circle1.setRadius(75.0f); circle1.setFill(Color.DARKRED); //Drawing Circle2 Circle circle2 = new Circle(); circle2.setCenterX(280.0f); circle2.setCenterY(170.0f); circle2.setRadius(75.0f); circle2.setFill(Color.DARKRED); //Drawing Circle3 Circle circle3 = new Circle(); circle3.setCenterX(330.0f); circle3.setCenterY(100.0f); circle3.setRadius(75.0f); circle3.setFill(Color.DARKRED); //Intersect Operation Shape subtract = Shape.subtract(circle1, circle2); subtract = Shape.intersect(subtract, circle3); subtract.setFill(Color.RED); //Setting the 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); } }
输出
广告