HOWTO · Java
How to Draw Rectangle in JavaFX
This tutorial demonstrates how to draw a rectangle in JavaFX.
On this page
In JavaFX, create a Rectangle, set its position, size, fill, and stroke, then add it to a Scene displayed by a Stage. The shape uses the scene’s coordinate system, with its origin at the upper-left. The JavaFX calls and property names below show the configuration to use in a JavaFX project. The configured Java runtime does not include JavaFX, so its graphical application cannot be compiled or run in this example environment.
Draw Rectangle in JavaFX
The Rectangle class is in the javafx.scene.shape package. Its x and y properties specify the upper-left corner, while width and height specify the dimensions. These properties take double values. A scene that is 800 by 400 units can contain a rectangle positioned at (200, 100) with a width of 400 and height of 200.
| Property | Setter | Description |
|---|---|---|
x |
setX(double) |
Horizontal position of the upper-left corner. |
y |
setY(double) |
Vertical position of the upper-left corner. |
width |
setWidth(double) |
Rectangle width. |
height |
setHeight(double) |
Rectangle height. |
arcWidth |
setArcWidth(double) |
Horizontal diameter used to round the corners. |
arcHeight |
setArcHeight(double) |
Vertical diameter used to round the corners. |
The JavaFX version uses rectangle.setX(200), rectangle.setY(100), rectangle.setWidth(400), and rectangle.setHeight(200). Set the interior with rectangle.setFill(Color.BLUE) and the outline with rectangle.setStroke(Color.LIGHTBLUE); rectangle.setStrokeWidth(8) controls the outline thickness. Add the shape to a Group, construct a Scene with the desired dimensions and background, assign the scene to a Stage, and call stage.show() from the application’s start method. The class extends Application and its main method calls launch(args). Run this version from a JavaFX project whose build or launch configuration makes the JavaFX modules available.
The following runnable Java2D program draws the same geometry into an in-memory image and reports the sampled fill color. It illustrates the position and size independently of JavaFX; it does not execute or display a JavaFX scene.
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
public class DrawRectangle {
public static void main(String[] args) {
System.setProperty("java.awt.headless", "true");
BufferedImage image = new BufferedImage(800, 400, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setColor(Color.BLUE);
graphics.fillRect(200, 100, 400, 200);
graphics.dispose();
Color sample = new Color(image.getRGB(250, 150));
System.out.printf("Rectangle: x=%d, y=%d, width=%d, height=%d%n", 200, 100, 400, 200);
System.out.printf("Sample fill: #%02X%02X%02X%n", sample.getRed(), sample.getGreen(), sample.getBlue());
}
}
Rectangle: x=200, y=100, width=400, height=200
Sample fill: #0000FF
To make the JavaFX shape rounded, set both arc properties after its dimensions: rectangle.setArcWidth(40) and rectangle.setArcHeight(40). setArcWidth controls the horizontal curve diameter, and setArcHeight controls the vertical curve diameter. Equal values give the corners an even curve; different values create an elongated curve. These settings affect the corners while leaving the rectangle’s position, overall dimensions, fill, and outline configured as before.