JavaFX Day - 1
JavaFX 2D shapes
The package javafx.scene.shape containes various classes that can be used to make 2D shapes in our JavaFX application.
Using javaFX we can create 2D shapes like Line, Rectangle, Circle, Ellipse, Polygon, Arc, etc.
How to create any 2D shape in JavaFX?
-
create an object of respective class :
Line line1 = new Line();
- Set the required properties using respective methods.
-
Add class object to Group layout using
Group root = new Group(); root.getChildren().add(line1);
The reason behind using Group layout:
It is a container component in which every child components are positioned at
0,0;
Shape | Class |
---|---|
Line | javafx.scene.shape.Line |
Rectangle | javafx.scene.shape.Rectangle |
Ellipse | javafx.scene.shape.Ellipse |
Arc | javafx.scene.shape.Arc |
Circle | javafx.scene.shape.Circle |
Polygon | javafx.scene.shape.Polygon |
Cubic Curve | javafx.scene.shape.CubicCurve |
Quad Curve | javafx.scene.shape.QuadCurve |
1. JavaFX Program that creates Diagonal on the Screen.
import javafx.application.Application; import javafx.geometry.Rectangle2D; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.shape.Line; import javafx.stage.Screen; import javafx.stage.Stage; public class DrawDiagonal extends Application { @Override public void start(Stage primaryStage) throws Exception { Rectangle2D screenBounds = Screen.getPrimary().getBounds(); System.out.println(screenBounds); Line line1 = new Line(); line1.setStartX(0); line1.setStartY(0); line1.setEndX(screenBounds.getMaxX()); line1.setEndY(screenBounds.getMaxY()); Group root = new Group(); root.getChildren().add(line1); Scene scene = new Scene(root, screenBounds.getWidth(), screenBounds.getHeight() - 80); primaryStage.setScene(scene); primaryStage.setTitle("DrawDiagonal by Kanu Thakor"); primaryStage.show(); } public static void main(String[] args) { // Here you can work with args - command line parameters Application.launch(args); } }
output
2. Javafx program that creates a rectangle with height and width slightly less than desktop's screen resolution
import javafx.application.Application; import javafx.geometry.Rectangle2D; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.shape.Line; import javafx.scene.shape.Rectangle; import javafx.stage.Screen; import javafx.stage.Stage; public class DrawRectangle extends Application { @Override public void start(Stage primaryStage) throws Exception { Rectangle2D screenBounds = Screen.getPrimary().getBounds(); System.out.println(screenBounds); Rectangle rect = new Rectangle(); rect.setX(20); rect.setY(20); rect.setWidth(screenBounds.getWidth() - 80); rect.setHeight(screenBounds.getHeight() - 100); Group root = new Group(); root.getChildren().add(rect); Scene scene = new Scene(root, screenBounds.getWidth(), screenBounds.getHeight() - 80); primaryStage.setScene(scene); primaryStage.setTitle("DrawDiagonal by Kanu Thakor"); primaryStage.show(); } public static void main(String[] args) { // Here you can work with args - command line parameters Application.launch(args); } }
Output
Comments