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 it
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.canvas.*;
import
javafx.scene.paint.Color;
import
javafx.scene.Group;
Â
Âpublic
class
canvas
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// set title for the stage
       Â
stage.setTitle(
"creating canvas"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a canvas
       Â
Canvas canvas =
new
Canvas(
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 =
new
Group(canvas);
Â
ÂÂ Â Â Â Â Â Â Â
// create a scene
       Â
Scene scene =
new
Scene(group,
200
,
200
);
Â
ÂÂ Â Â Â Â Â Â Â
// set the scene
       Â
stage.setScene(scene);
Â
ÂÂ Â Â Â Â Â Â Â
stage.show();
   Â
}
Â
ÂÂ Â Â Â
// Main Method
   Â
public
static
void
main(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 it
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.canvas.*;
import
javafx.scene.paint.Color;
import
javafx.scene.Group;
Â
Âpublic
class
canvas1
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// set title for the stage
       Â
stage.setTitle(
"creating canvas"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a canvas
       Â
Canvas canvas =
new
Canvas();
Â
ÂÂ Â Â Â Â Â Â Â
// 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 =
new
Group(canvas);
Â
ÂÂ Â Â Â Â Â Â Â
// create a scene
       Â
Scene scene =
new
Scene(group,
400
,
400
);
Â
ÂÂ Â Â Â Â Â Â Â
// set the scene
       Â
stage.setScene(scene);
Â
ÂÂ Â Â Â Â Â Â Â
stage.show();
   Â
}
Â
ÂÂ Â Â Â
// Main Method
   Â
public
static
void
main(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