JavaFX | Canvas Class

Canvas class is a part of JavaFX. Canvas class basically creates an image that can be drawn on using a set of graphics commands provided by a GraphicsContext. Canvas has a specified height and width and all the drawing operations are clipped to the bounds of the canvas.
Constructors of the class:
- Canvas(): Creates a new canvas object.
- Canvas(double w, double h): Creates a new canvas object with specified width and height.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getGraphicsContext2D() | Returns the graphics context associated with the canvas. |
| getHeight() | Returns the height of the canvas. |
| getWidth() | Returns the width of the canvas. |
| setHeight(double v) | Sets the height of the canvas. |
| setWidth(double d) | Sets the width of the canvas. |
Below programs illustrate the use of Canvas class:
- Java Program to create a canvas with specified width and height(as arguments of constructor), add it to the stage and also add a circle and rectangle on it: In this program we will create a Canvas named canvas with specified width and height. We will extract the GraphicsContext using the getGraphicsContext2D() function and draw a rectangle and a oval of different color. Now we will create a Group named group and add the canvas to the group. Now create a scene and add the group to the scene and then attach the scene to the stage and call the show() function to display the results.
// Java Program to create a canvas with specified// width and height(as arguments of constructor),// add it to the stage and also add a circle and// rectangle on itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.paint.Color;importjavafx.scene.Group;publicclasscanvasextendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating canvas");// create a canvasCanvas canvas =newCanvas(100.0f,100.0f);// graphics contextGraphicsContext graphics_context =canvas.getGraphicsContext2D();// set fill for rectanglegraphics_context.setFill(Color.RED);graphics_context.fillRect(20,20,70,70);// set fill for ovalgraphics_context.setFill(Color.BLUE);graphics_context.fillOval(30,30,70,70);// create a GroupGroup group =newGroup(canvas);// create a sceneScene scene =newScene(group,200,200);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
- Java Program to create a canvas and use setHeight() and setWidth() function to set canvas size and add it to the stage and also add a circle and rectangle on it: In this program we will create a Canvas named canvas and set the width and height using the setWidth() and setHeight() function. We will extract the GraphicsContext using the getGraphicsContext2D() function and draw two rectangles and a oval of different color. We will create a Group named group and add the canvas to the group. We will create a scene and add the group to the scene and then attach the scene to the stage. Finally, call the show() function to display the results.
// Java Program to create a canvas and use// setHeight() and setWidth() function to// set canvas size and add it to the stage// and also add a circle and rectangle on itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.paint.Color;importjavafx.scene.Group;publicclasscanvas1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating canvas");// create a canvasCanvas canvas =newCanvas();// set height and widthcanvas.setHeight(400);canvas.setWidth(400);// graphics contextGraphicsContext graphics_context =canvas.getGraphicsContext2D();// set fill for rectanglegraphics_context.setFill(Color.PINK);graphics_context.fillRect(40,40,100,100);// set fill for rectanglegraphics_context.setFill(Color.RED);graphics_context.fillRect(20,20,70,70);// set fill for ovalgraphics_context.setFill(Color.BLUE);graphics_context.fillOval(30,30,70,70);// create a GroupGroup group =newGroup(canvas);// create a sceneScene scene =newScene(group,400,400);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/canvas/Canvas.html




