64 JavaFX 2D
The JavaFX 2D shape classes enables you to add 2D graphical shapes to the JavaFX scene graph, just as if they were any other type of JavaFX control. This enables you to draw e.g. circles or squares on top of your GUI, or create new JavaFX controls composed from 2D shapes and other more regular controls etc.
Keep in mind that JavaFX also has the JavaFX Canvas control which you can draw 2D graphics on. The main difference is, that a Canvas is a single JavaFX Node regardless of how many shapes you draw on it, whereas each JavaFX 2D shape is a separate control.
JavaFX 2D Example
Here is a quick example that shows you how to use the JavaFX 2D shapes in the JavaFX scene graph.
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class Gfx2DExample extends Application { public static void main(String[] args) { launch(args); } public void start(Stage primaryStage) { Circle circle = new Circle(); circle.setCenterX(100); circle.setCenterY(100); circle.setRadius(125); circle.setStroke(Color.valueOf("#ff00ff")); circle.setStrokeWidth(5); circle.setFill(Color.TRANSPARENT); Rectangle rectangle = new Rectangle(); rectangle.setX(200); rectangle.setY(200); rectangle.setWidth(300); rectangle.setHeight(400); rectangle.setStroke(Color.TRANSPARENT); rectangle.setFill(Color.valueOf("#00ffff")); Pane pane = new Pane(); pane.getChildren().add(circle); pane.getChildren().add(rectangle); Scene scene = new Scene(pane, 1024, 800, true); primaryStage.setScene(scene); primaryStage.setTitle("2D Example"); primaryStage.show(); } }
Next: JavaFX Effects