Example
In this example, we will draw two circles and perform the subtraction operation.
Code
import javafx.scene.paint.Color;
import javafx.application.Application;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Shape;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class App extends Application {
// launch application
public void start(Stage stage) {
// set the title of the stage
stage.setTitle("Example of 2D Shape subtraction operation");
// Drawing circle1
Circle circle1 = new Circle();
// Setting the position of the center of circle1
circle1.setCenterX(200.0f);
circle1.setCenterY(100.0f);
// setting the radius of the circle1
circle1.setRadius(100.0f);
// setting the colour of the circle1
circle1.setFill(Color.GREENYELLOW);
// Drawing Circle2
Circle circle2 = new Circle();
// Setting the position of the circle2
circle2.setCenterX(320.0f);
circle2.setCenterY(100.0f);
// setting the radius of the circle2
circle2.setRadius(100.0f);
// setting the colour of the circle2
circle2.setFill(Color.BLUE);
// Performing subtraction operation on the circle
Shape shape = Shape.subtract(circle1, circle2);
// Setting the colour to the subtracted area
shape.setFill(Color.GREENYELLOW);
// Creating a Group object
Group group = new Group(circle1, circle2);
VBox vb = new VBox(group, shape);
// Creating a scene object
Scene scene = new Scene(vb, 400, 500);
// Adding scene to the stage
stage.setScene(scene);
// Displaying the result
stage.show();
}
public static void main(String args[]) {
// launch the application
launch(args);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

Explanation
The explanation of the above code is shown below:
- First, we created two circles using the constructor circle().
- Then we set the centre of the circles using setCenterX() and setCenterY() methods, set the radius of the circles using the setRadius() method, and set the colour of the circle using the setFill() method.
- After creating and colouring both the circles, it’s time to subtract one circle from another. For this purpose, we used Shape.subtract().
- After completing the subtraction operation, we set the colour of the subtracted area using the setFill() method.
- Finally, We displayed the result on the stage by creating a scene and adding the scene to the stage.
Frequently Asked Questions
What do you understand by JavaFX?
JavaFX is an open-source java library that enables developers to create desktop, mobile, browser and rich client applications.
Name the three main components of the JavaFX Application.
The three major components of a JavaFX application are Stage, Scene and Nodes.
What do you mean by the 2D Shape subtraction operation in JavaFX?
In JavaFX, the 2D shape subtraction operation takes two or more shapes as an input and returns an area in the first shape, excluding the area overlapped by the second shape.
Name the function used to perform the subtraction operation between two 2D shapes in JavaFX.
In JavaFX, We can perform the subtraction operation between two 2D shapes using the subtract() method.
Conclusion
In this article, we have extensively discussed the 2D Shapes(Objects) Subtraction Operation in JavaFX.
We hope that this blog gives you some ideas regarding 2D Shapes(Objects) Subtraction Operation in JavaFX.If you want to learn more about JavaFX, follow these articles, JavaFX Label, JavaFX CheckBox, JavaFX RadioButton, JavaFX Tooltip and Introduction to JavaFX Effects.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, Machine learning, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!
Happy Reading!
