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 application   Âpublicvoidstart(Stage stage)   Â{       Â// set title for the stage       Âstage.setTitle("creating canvas");       Â// create a canvas       ÂCanvas canvas =newCanvas(100.0f,100.0f);       Â// graphics context       ÂGraphicsContext graphics_context =            Âcanvas.getGraphicsContext2D();       Â// set fill for rectangle       Âgraphics_context.setFill(Color.RED);       Âgraphics_context.fillRect(20,20,70,70);       Â// set fill for oval       Âgraphics_context.setFill(Color.BLUE);       Âgraphics_context.fillOval(30,30,70,70);       Â// create a Group       ÂGroup group =newGroup(canvas);       Â// create a scene       ÂScene scene =newScene(group,200,200);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(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 application   Âpublicvoidstart(Stage stage)   Â{       Â// set title for the stage       Âstage.setTitle("creating canvas");       Â// create a canvas       ÂCanvas canvas =newCanvas();       Â// set height and width       Âcanvas.setHeight(400);       Âcanvas.setWidth(400);       Â// graphics context       ÂGraphicsContext graphics_context =           Âcanvas.getGraphicsContext2D();       Â// set fill for rectangle       Âgraphics_context.setFill(Color.PINK);       Âgraphics_context.fillRect(40,40,100,100);       Â// set fill for rectangle       Âgraphics_context.setFill(Color.RED);       Âgraphics_context.fillRect(20,20,70,70);       Â// set fill for oval       Âgraphics_context.setFill(Color.BLUE);       Âgraphics_context.fillOval(30,30,70,70);       Â// create a Group       ÂGroup group =newGroup(canvas);       Â// create a scene       ÂScene scene =newScene(group,400,400);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(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




