Table of contents
1.
Introduction
2.
JavaFX Shape Properties
2.1.
Common Shape Properties
2.2.
Example
2.3.
Code Explanation
2.4.
Output
3.
Frequently Asked Questions
3.1.
What is stroke in JavaFX?
3.2.
In JavaFX, how can we alter the colour of a shape?
3.3.
In JavaFX, how do we make a scene object?
4.
Conclusion
Last Updated: Mar 27, 2024

JavaFX Shape Properties

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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);
	}
}
You can also try this code with Online Java Compiler
Run Code

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.

Frequently Asked Questions

What is stroke in JavaFX?

The stroke attribute specifies/defines the colour of a shape's boundary. We can change the boundary's colour using the javafx setStroke() method.

In JavaFX, how can we alter the colour of a shape?

The setFill() and setStroke() functions in JavaFX can be used to apply colours to nodes. The setFill() method adds colour to the node's surface area. On the other hand, the setStroke() method colours the node's boundaries. Both methods accept a javafx.scene object.

In JavaFX, how do we make a scene object?

The "Scene" represents the scene object. In this case, the scene item is added to only one stage. We can make a scene by instantiating the Scene Class. We can specify the scene's size by supplying its dimensions (height and width) and the root node to its function Object.

Conclusion

The JavaFX shape properties make the uses of shapes more modular. We can look into it and easily understand how it makes using different 2D shapes more versatile. In the above article, we read about what JavaFX Shape Properties are. We looked into some common properties and their setter methods. We also implemented an example and found out how we can use them. Explore our courses on HTML and CSS here for free. We have some similar articles like JavaFX Arc and JavaFX Polyline for you along with an article on JavaFX Rectangle. You can check out various Java topics and blogs on JavaFX to follow.


Explore Coding Ninjas Studio to find more exciting stuff. Happy Coding!
 

Live masterclass