
- 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 custom shapes in JavaFX using the Path class?
The javafx.scene.shape package provides classes using which you can draw various 2D shapes, but these are just primitive shapes like line, circle, polygon, and ellipse, etc…
Therefore, if you want to draw complex custom shapes you need to use the Path class.
The Path Class
The Path class represents the geometrical outline of a shape and is attached to an observable list which holds various Path Elements such as MoveTo, LineTo, HlineTo, VlineTo, ArcTo, QuadCurveTo, CubicCurveTo.
The constructor of this class accepts variable arguments of the type PathElement and constructs a path based on the given path elements.
Example
The Path Element MoveTo is used to move the current position of the path to a specified point and the LineTo draws a line from current coordinates to the specified ones.
In the following example, we are trying to draw a custom complex shape using the MoveTo PathTo and Path classes −
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.LineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; public class ComplexShape extends Application { public void start(Stage stage) { //Drawing the shape MoveTo moveTo = new MoveTo(108, 71); LineTo line1 = new LineTo(321, 161); LineTo line2 = new LineTo(126,232); LineTo line3 = new LineTo(232,52); LineTo line4 = new LineTo(269, 250); LineTo line5 = new LineTo(108, 71); //Creating a Path Path path = new Path(moveTo, line1, line2, line3, line4, line5); //Preparing the Stage object Group root = new Group(path); Scene scene = new Scene(root, 600, 300); stage.setTitle("Drawing an arc through a path"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
- Related Articles
- How to draw shapes using SVG in HTML5?
- How to draw different shapes using the Python Turtle library?
- How to Create the path element line in JavaFX?
- How to Create the path element arc in JavaFX?
- How to draw geometrical shapes on image using OpenCV Java Library?
- How to Create the path element horizontal line in JavaFX?
- How to Create the path element vertical line in JavaFX?
- How to Create the path element quadratic curve in JavaFX?
- How to Create the path element cubic curve in JavaFX?
- How to add custom fonts to a text 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
