
- 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
How to draw a geometrical 2D shape in JavaFX?
In general, a 2D shape is a geometrical figure that can be drawn on the XY plane, these include Line, Rectangle, Circle, etc.
The javafx.scene.shape package provides you, various classes, each of them represents/defines a 2d geometrical object or, an operation on them. The class named Shape is the base class of all the 2-Dimensional shapes in JavaFX.
Creating 2D shapes
To draw a 2D geometrical shape using JavaFX you need to −
Instantiate the class − Instantiate the respective class. For example, if you want to draw a circle you need to instantiate the Circle class as shown below −
//Drawing a Circle Circle circle = new Circle();
Set the properties − Set properties of the shape using the method of its respective class. For example, To draw a circle you need the center and radius and you can set them using the setCenterX(), setCenterY() and setRadius() methods respectively.
//Setting the properties of the circle circle.setCenterX(300.0f); circle.setCenterY(135.0f); circle.setRadius(100.0f);
Add the shape object to the group − Finally, pass the shape created as a parameter to the group constructor as −
Group root = new Group(circle);
Example
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.Circle; public class CircleExample extends Application { public void start(Stage stage) { //Drawing a Circle Circle circle = new Circle(); //Setting the properties of the circle circle.setCenterX(300.0f); circle.setCenterY(135.0f); circle.setRadius(100.0f); //Creating a Group object Group root = new Group(circle); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle("Drawing a Circle"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } }
Output
- Related Articles
- How to draw geometrical shapes on image using OpenCV Java Library?
- How to draw close shape in android?
- How to draw love shape in android?
- How to draw star shape in android?
- How to draw triangle shape in android?
- How to draw profile icon shape in android?
- Explain the Properties of 2D objects in JavaFX
- How to draw custom shapes in JavaFX using the Path class?
- What are various operations of 2D objects in JavaFX?
- Explain the Union operation on 2D shapes in JavaFX
- Explain the Intersect operation on 2D shapes in JavaFX
- Explain the Subtract operation on 2D shapes in 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
