
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What are various operations of 2D objects in JavaFX?
JavaFX supports three operations on 2D objects namely – Union, Subtraction and Intersection.
Union Operation − This operation takes two or more shapes as inputs and returns the area occupied by them.
Intersection Operation − This operation takes two or more shapes as inputs and returns the intersection area between them.
Subtraction Operation − 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.
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; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class JavaFXOperations extends Application { public void start(Stage stage) { Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12); //Drawing circles for union operation Circle shape1 = new Circle(65.0f, 135.0f, 50.0f); Circle shape2 = new Circle(130.0f, 135.0f, 50.0f ); //Union operation Shape union = Shape.union(shape1, shape2); union.setFill(Color.RED); Text label1 = new Text("Union Operation"); label1.setFont(font); label1.setX(40); label1.setY(230); //Drawing circles for union operation Circle shape3 = new Circle(250.0f, 135.0f, 50.0f); Circle shape4 = new Circle(325.0f, 135.0f, 50.0f ); //Intersect operation Shape intersect = Shape.intersect(shape3, shape4); intersect.setFill(Color.RED); Text label2 = new Text("Intersect Operation"); label2.setFont(font); label2.setX(225); label2.setY(230); //Drawing circles for union operation Circle shape5 = new Circle(445.0f, 135.0f, 50.0f); Circle shape6 = new Circle(510.0f, 135.0f, 50.0f ); //Union operation Shape subtract = Shape.subtract(shape5, shape6); subtract.setFill(Color.RED); Text label3 = new Text("Subtract Operation"); label3.setFont(font); label3.setX(420); label3.setY(230); //Setting the stage Group root = new Group( shape1, shape2, shape3, shape4, shape5, shape6, union, intersect, subtract, label1, label2, label3 ); Scene scene = new Scene(root, 600, 300); stage.setTitle("2D Operations Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
- Related Articles
- What are various 2D shapes provided by JavaFX?
- Explain the Properties of 2D objects in JavaFX
- What are various path elements in JavaFX?
- What are various 3D shapes provided by JavaFX?
- What are various XY charts provided by JavaFX?
- Explain the Fill property of 2D shapes in JavaFX
- Explain the Stroke property of 2D shapes in JavaFX
- Explain the smooth property of 2D shapes in JavaFX
- Explain the Stroke Type property of 2D shapes in JavaFX
- Explain the Stroke Width property of 2D shapes in JavaFX
- Explain the Stroke Line Join property of 2D shapes in JavaFX
- Explain the Stroke Miter Limit property of 2D shapes in JavaFX
- Explain the Stroke Line Cap property of 2D shapes in JavaFX
- Explain the stroke Dash Offset property of 2D shapes in JavaFX
- How to draw a geometrical 2D shape in JavaFX?

Advertisements