Javafx 简明教程

JavaFX - Union Operation

从数学角度看,并集运算在集合论的概念中得到广泛应用。此运算将两个不同的集合组合为一个集合。在此处,一个集合中的所有元素都会合并到另一个集合中,无论是否存在重复元素。然后,这个概念会由计算机编程中的各种技术采用。

If you look at it mathematically, the Union operation was fundamentally used in the concept of set theory. This operation is defined as the combination of two different sets into one. In here, all the elements of one set and merged within another set, regardless of any duplicities. This concept is then adopted by various techniques in computer programming.

例如,SQL 中执行的并集运算将两个或多个结果集组合为一个常见结果集。它还会用作 C、C++、Java、Python 等编程语言中的运算符或方法。

For instance, union operation when performed in SQL combines two or more result-sets into one common result set. It is also used in programming languages like C, C++, Java, Python etc. as an operator or a method.

同样,JavaFX 也对 2D 形状提供了并集运算。

Similarly, JavaFX also provides union operation on 2D shapes.

Union Operation in JavaFX

JavaFX 为 2D 形状提供的并集运算。在此处,两个或多个形状的区域会被组合在一起,以形成一个更大、更复杂的形状。因此,并集运算将两个或多个形状作为输入,然后返回它们所占据的组合区域,如下所示。

JavaFX provides union operation which can be performed on 2D shapes. In here, the areas of two or more shapes are combined together to form a bigger and a more complex shape. Thus, the union operation takes two or more shapes as inputs and returns the combined area occupied by them as shown below.

union operation

您可以使用名为 union() 的方法对形状执行并集运算。由于这是一个静态方法,因此您应该使用类名称(Shape 或其子类)对其进行调用,如下所示。

You can perform union operation on the shapes using the method called union(). Since this is a static method, you should call it using the class name (Shape or its subclasses) as shown below.

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

Example

下面是并集运算的一个示例。在此处,我们正在绘制两个圆,并对它们执行并集运算。将此代码保存在名为 unionExample.java 的文件中。

Following is an example of the union operation. In here, we are drawing two circles and performing a union operation on them. Save this code in a file with the name unionExample.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 UnionExample 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 union operation on the circle
      Shape shape = Shape.union(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("Union 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 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls UnionExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls UnionExample

执行上述程序时,生成一个 JavaFX 窗口,显示以下输出 −

On executing, the above program generates a JavaFX window displaying the following output −

union operation circle

Example

让我们尝试对圆以外的形状(如椭圆)执行并集运算。将此文件保存在名为 EllipseUnionOperation.java 的名称下。

Let us try to perform union operation on shapes other than a circle, like an ellipse. Save this file under the name EllipseUnionOperation.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 EllipseUnionOperation 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.union(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("Union 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 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls EllipseUnionOperation.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls EllipseUnionOperation

执行上述程序时,生成一个 JavaFX 窗口,显示以下输出 −

On executing, the above program generates a JavaFX window displaying the following output −

ellipse union