Introduction
In some applications, we need to present the user with two-dimensional forms. JavaFX gives us the freedom to draw 2D shapes on the screen. However, such shapes may need to be modified to meet our specifications. As a result, in our application, we must assign different properties to our forms. We may use JavaFX to build 2D shapes such as lines, rectangles, circles, ellipses, polygons, cubic curves, quad curves, arches, and so on. The primary class for all shape classes is ".shape". All JavaFX 2D shape classes inherit the JavaFX.scene.shape class's common properties. The usual shape attributes are described further in the article.
Let us know more about JavaFX shape properties.
JavaFX Shape Properties
The Shape class defines common attributes for objects representing some type of geometric shape. These attributes can be seen as: 'The Paint', applied to the shape's fillable interior (Example: setFill). The Paint should be used to stroke the outline of the shape (Example: setStroke).
- There are also some ornamental features of the strokes; they are as follows:
- The stroke width of the border. Which would be the width of the stroke implemented.
- The Stroke Type of the shape made. Defines whether the border is drawn as outside padding to the shape's edges. This property can also be an interior edging that follows the inside of the shape's boundaries. It is also a wide path that follows along the edge, straddling it both inside and outside. It must be consistent.
- The Decoration styles for path segment join and path ends that are not closed.
Common Shape Properties
The JavaFX Shape properties, descriptions, and Setter Methods are listed in the table below.

Example
We will look into an example where we will use some of the JavaFX Shape Properties.
package application;
import javafx.scene.shape.StrokeLineJoin;
import javafx.application.Application;
import javafx.scene.shape.StrokeType;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class ShapeProperties extends Application {
public void start (Stage primaryStage) {
Pane pane = new Pane();
Rectangle rect1 = new Rectangle(25, 10, 60, 30);
rect1.setStroke(Color.BLACK);
rect1.setFill(Color.RED);
pane.getChildren().add(new Text(10,27, "r1"));
pane.getChildren().add(rect1);
Rectangle rect2 = new Rectangle(25, 50, 60 , 30);
rect2.setStroke(Color.BLACK);
rect2.setStrokeType(StrokeType.INSIDE);
rect2.setStrokeWidth(10);
rect2.setFill(Color.TRANSPARENT);
pane.getChildren().add(new Text(10,67, "r2"));
pane.getChildren().add(rect2);
Rectangle rect3 = new Rectangle(25, 90, 60, 30);
rect3.setArcWidth(15);
rect3.setArcHeight(25);
rect3.setStroke(Color.BLACK);
rect3.setFill(Color.YELLOW);
pane.getChildren().add(new Text(10,107, "r3"));
pane.getChildren().add(rect3);
Rectangle rect4 = new Rectangle(30, 140, 60, 30);
rect4.setStroke(Color.BLUE);
rect4.setFill(Color.PINK);
rect4.setStrokeWidth(15);
rect4.setStrokeLineJoin(StrokeLineJoin.ROUND);
pane.getChildren().add(new Text(10,157, "r4"));
pane.getChildren().add(rect4);
Scene scene = new Scene(pane, 250, 150);
primaryStage.setTitle("ShowRectangle");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}Code Explanation
From the above code, we will get an output displaying a rectangle with different JavaFX shape properties. It shows how various properties can be implemented on the same shape. For a start, we should look at the parts of the code which define the position of the rectangles. It should be kept in mind that the X and Y positions should not overlap each other. Otherwise, one shape will overlap the other and cover it.
As we can see, the first rectangle, r1, has a red colour fill and a black border. This has been done using the setFill and setStroke JavaFX shape properties.
The second rectangle, r2, has a thick black-coloured outline. This is done using StrokeWidth JavaFX Shape Property, and the inside has been made transparent using the Transparent fill. The StrokeType is set inside the rectangle made.
In the third rectangle, r3, we see the shape's sides to be slightly curved. This is done using the setArcWidth and setArcWidth JavaFX properties. This also has the setFill JavaFX Shape property that has made the shape get a yellow fill.
Looking at the fourth rectangle, r4, we used the setStrokeLineJoin JavaFX shape property. This has made the corners of the shape more rounded. The thickness of the shape also can be seen to be made more prominent. This has been again done using the setStrokeWidth JavaFX shape property.
Output

Looking at the above output, we can quickly figure out that we can change the JavaFX Shape properties and make the same shape appear differently.




