Table of contents
1.
Introduction
2.
What is 2D Shape Subtraction Operation?
3.
Example
3.1.
Code
3.2.
Output
3.3.
Explanation
4.
Frequently Asked Questions
4.1.
What do you understand by JavaFX?
4.2.
Name the three main components of the JavaFX Application.
4.3.
What do you mean by the 2D Shape subtraction operation in JavaFX?
4.4.
Name the function used to perform the subtraction operation between two 2D shapes in JavaFX.
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

2D Shapes(Objects) Subtraction Operations in JavaFX

Author Aniket Majhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello and welcome, readers! We hope that you are doing well.

JavaFX is the Java library consisting of graphics and media packages for developing desktop applications and rich client applications that can operate across multiple platforms.

JavaFX image

 

Today in this article, we will discuss the 2D shapes(Objects) Subtraction Operations in JavaFX. We will explain the 2D shapes Subtraction operation in JavaFX, and then we will show one example of performing the 2D shape subtraction operation in JavaFX. So. follow the article till the end.

So, without further ado, let’s start our discussion.

What is 2D Shape Subtraction Operation?

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.

For example, let’s consider the below examples where we have considered two circles performed 2D shape subtracted operation on them.

Visual representation of the question "What is 2D shape subtraction operation"

We can perform the subtraction operation on the 2D shapes as shown above using subtract() method. The subtract() method is a static method, so you should call this method using the class name as shown below.

Shape shape = Shape.subtract(circle1, circle2);
You can also try this code with Online Java Compiler
Run Code

 

Let’s now see one example of this subtraction method.

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

Output of the code to perform 2D shape subtraction operation.

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 CheckBoxJavaFX RadioButtonJavaFX Tooltip and Introduction to JavaFX Effects.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem DesignMachine 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 problemsinterview 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!

Live masterclass