![]() | Chapter 7 | ![]() |
Canvas
Coming back to the painting, artists have been painting their masterpieces on the canvas. A canvas is also available in JavaFX - Canvas
class.
Here is our black square, but with a canvas:
1 2 3 4 5 6 7 8 9 10 11 |
public void start(Stage primaryStage) { Group root = new Group(); // create a root node Canvas canvas = new Canvas( 535 , 535 ); // create a canvas GraphicsContext gc = canvas.getGraphicsContext2D(); // graphics content gc.fillRect( 50 , 50 , 435 , 435 ); // square root.getChildren().add(canvas); // place a canvas on the scene Scene scene1 = new Scene(root, 535 , 535 , Color.SNOW); // create a scene primaryStage.setTitle( "Black square" ); // set the stage title primaryStage.setScene(scene1); // place the scene on the stage primaryStage.show(); // display the stage } |
Useful link: more examples of working with the canvas can be found here.
![]() | Chapter 7 | ![]() |