Javafx 简明教程

JavaFX - Subtraction Operation

顾名思义,减法运算将减去集合中的元素在另一个集合中元素。大多数人通常会对交集运算和减法运算感到困惑,两个运算在其运算方式上完全不同。交集运算检索两个集合中公共的元素,而减法运算查找两个集合中的公共元素并将其从第一个集合中移除。如果第二个集合中存在第一个集合中不存在的元素,则将忽略它们。

As the name suggests, the subtraction operation will subtract the elements of a set from another set. Most people are generally confused between the intersection operation, the two operations completely differ with respect to their operations. While intersection operation retrieves the common elements between two sets, the subtraction operation finds the common elements between two sets and removes them from the first set. If there are elements in second set that are not present in the first set, they are ignored.

和其他运算类似,减法运算也在计算机编程中应用。它在一些编程语言中可用作差分运算符;但在 JavaFX 中,此运算可用于 2D 形状。

Like other operations, the subtraction operation is also adopted in computer programming. It is available as difference operator in few programming languages; but in JavaFX, this operation can be used on 2D shapes.

Subtraction Operation in JavaFX

在 JavaFX 中,减法运算涉及两个或多个 2D 形状覆盖的区域。它从第一个形状的区域中消除第二个形状的区域。如果两个形状的区域完全互斥,则第一个形状的区域将保留为结果。从技术上讲,此运算将两个或多个形状作为输入。然后,它返回第一个形状减去如下所示的第二个形状重叠区域的区域。

In JavaFX, the subtraction operation works with the area covered by two or more 2D shapes. It eliminates the area of the second shape from the area of the first shape. If the areas of these two shapes are fully exclusive, the area of first shape is retained as the result. Technically, this operation takes two or more shapes as an input. Then, it returns the area of the first shape excluding the area overlapped by the second one as shown below.

subtraction operation

你可以使用名为 subtract() 的方法在形状上执行减法运算。因为这是一个静态方法,所以你应使用类名(Shape 或其子类)调用它,如下所示。

You can perform the Subtraction Operation on the shapes using the method named subtract(). 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);

以下是减法运算的示例。在此示例中,我们绘制了两个圆并对它们执行了减法运算。

Following is an example of the Subtraction Operation. In here, we are drawing two circles and performing a subtraction operation on them.

将此代码保存在名为 SubtractionExample.java 的文件中。

Save this code in a file with name SubtractionExample.java.

Example

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 SubtractionExample 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 subtraction operation on the circle
      Shape shape = Shape.subtract(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("Subtraction 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 SubtractionExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls SubtractionExample

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

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

subtraction operation output

Example

现在,我们尝试在两个椭圆上执行减法运算,我们从第一个椭圆中减去第二个椭圆的区域。将此文件保存在名为 EllipseSubtractionOperation.java 的文件中。

Now, let us try to perform subtraction operation on two ellipses where we will subtract the area of second ellipse from the first ellipse. Save this file under the name EllipseSubtractionOperation.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 EllipseSubtractionOperation 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.subtract(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("Subtraction 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 EllipseSubtractionOperation.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls EllipseSubtractionOperation

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

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

ellipse subtract