JavaFX Canvas Example
This is a JavaFX Canvas Example. Through the javafx.scene.canvas package, JavaFX provides the Canvas API that offers a drawing surface to draw shapes, images, and text using drawing commands. The API also gives pixel-level access to the drawing surface where you can write any pixels on the surface. The API consists of only two classes:
- Canvas
- GraphicsContext
A canvas is a bitmap image, which is used as a drawing surface. An instance of the Canvas class represents a canvas. It inherits from the Node class. Therefore, a Canvas is a Node.
It can be added to a Scene Graph, and effects and transformations can be applied to it. A Canvas has a graphics context associated with it that is used to issue drawing commands to the Canvas. An instance of the GraphicsContext class represents a graphics context.
The following table shows an overview of the whole article:
Table Of Contents
The following examples uses Java SE 8 and JavaFX 2.2.
1. Creating a Canvas
1.1 The Code
FxCanvasExample1.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class FxCanvasExample1 extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
@Override
public void start(Stage stage)
{
// Create the Canvas
Canvas canvas = new Canvas(400, 200);
// Set the width of the Canvas
canvas.setWidth(400);
// Set the height of the Canvas
canvas.setHeight(200);
// Get the graphics context of the canvas
GraphicsContext gc = canvas.getGraphicsContext2D();
// Draw a Text
gc.strokeText("Hello Canvas", 150, 100);
// Create the Pane
Pane root = new Pane();
// Set the Style-properties of the Pane
root.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");
// Add the Canvas to the Pane
root.getChildren().add(canvas);
// Create the Scene
Scene scene = new Scene(root);
// Add the Scene to the Stage
stage.setScene(scene);
// Set the Title of the Stage
stage.setTitle("Creation of a Canvas");
// Display the Stage
stage.show();
}
}
The Canvas class has two constructors. The no-args constructor creates an empty canvas. Later, you can set the size of the canvas using its width and height properties. The other constructor takes the width and height of the canvas as parameters:
// Create a Canvas of zero width and height Canvas canvas = new Canvas(); // Create a 400X200 canvas Canvas canvas = new Canvas(400, 200);
1.2 The GUI
The following image shows the result of the above example:

2. Drawing on the Canvas
2.1 Introduction
Once you create a canvas, you need to get its graphics context using the getGraphicsContext2D() method, as in the following snippet of code:
// Get the graphics context of the canvas GraphicsContext gc = canvas.getGraphicsContext2D();
All drawing commands are provided in the GraphicsContext class as methods. Drawings that fall outside the bounds of the Canvas are clipped. The canvas uses a buffer. The drawing commands push necessary parameters to the buffer. It is important to note that you should use the graphics context from any one thread before adding the Canvas to the Scene Graph.
Once the Canvas is added to the Scene Graph, the graphics context should be used only on the JavaFX Application Thread. The GraphicsContext class contains methods to draw the following types of objects:
- Basic shapes
- Text
- Paths
- Images
- Pixels
2.2 Drawing Basic Shapes
2.2.1 The Code
FxCanvasExample2.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.ArcType;
import javafx.stage.Stage;
public class FxCanvasExample2 extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
@Override
public void start(Stage stage)
{
// Create the Canvas with a width of 400 px and a height of 200 px.
Canvas canvas = new Canvas(400, 200);
// Get the graphics context of the canvas
GraphicsContext gc = canvas.getGraphicsContext2D();
// Set line width
gc.setLineWidth(2.0);
// Set fill color
gc.setFill(Color.RED);
// Draw a rounded Rectangle
gc.strokeRoundRect(10, 10, 50, 50, 10, 10);
// Draw a filled rounded Rectangle
gc.fillRoundRect(100, 10, 50, 50, 10, 10);
// Change the fill color
gc.setFill(Color.BLUE);
// Draw an Oval
gc.strokeOval(10, 70, 50, 30);
// Draw a filled Oval
gc.fillOval(100, 70, 50, 30);
// Draw a Line
gc.strokeLine(200, 50, 300, 50);
// Draw an Arc
gc.strokeArc(320, 10, 50, 50, 40, 80, ArcType.ROUND);
// Draw a filled Arc
gc.fillArc(320, 70, 50, 50, 00, 120, ArcType.OPEN);
// Create the Pane
Pane root = new Pane();
// Set the Style-properties of the Pane
root.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");
// Add the Canvas to the Pane
root.getChildren().add(canvas);
// Create the Scene
Scene scene = new Scene(root);
// Add the Scene to the Stage
stage.setScene(scene);
// Set the Title of the Stage
stage.setTitle("Drawing Basic Shapes on a Canvas");
// Display the Stage
stage.show();
}
}
The GraphicsContext class provides two types of methods to draw the basic shapes. The method fillXxx() draws a shape Xxx and fills it with the current fill paint. The method strokeXxx() draws a shape Xxx with the current stroke. Use the following methods for drawing shapes:
- fillArc()
- fillOval()
- fillPolygon()
- fillRect()
- fillRoundRect()
- strokeArc()
- strokeLine()
- strokeOval()
- strokePolygon()
- strokePolyline()
- strokeRect()
- strokeRoundRect()
The following snippet of code draws a rounded rectangle. The stroke color is red and the stroke width is 2px. The upper-left corner of the rectangle is at (10, 10). The rectangle is 50px wide and 50px high. The arcWidth and the arcHeight are 10 px.
// Create the Canvas with a width of 400 px and a height of 200 px. Canvas canvas = new Canvas(400, 200); // Get the graphics context of the canvas GraphicsContext gc = canvas.getGraphicsContext2D(); // Set line width gc.setLineWidth(2.0); // Set fill color gc.setFill(Color.RED); // Draw a rounded Rectangle gc.strokeRoundRect(10, 10, 50, 50, 10, 10);
2.2.2 The GUI
The following image shows a canvas with a few basic shapes (rectangular, oval, etc.):

2.3 Drawing Text
2.3.1 The Code
FxCanvasExample3.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class FxCanvasExample3 extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
@Override
public void start(Stage stage)
{
// Create the Canvas
Canvas canvas = new Canvas(400, 200);
// Get the graphics context of the canvas
GraphicsContext gc = canvas.getGraphicsContext2D();
// Set line width
gc.setLineWidth(1.0);
// Set fill color
gc.setFill(Color.BLUE);
// Draw a Text
gc.strokeText("This is a stroked Text", 10, 50);
gc.strokeText("This is a stroked Text with Max Width 300 px", 10, 100, 300);
// Draw a filled Text
gc.fillText("This is a filled Text", 10, 150);
gc.fillText("This is a filled Text with Max Width 400 px", 10, 200, 400);
// Create the Pane
Pane root = new Pane();
// Set the Style-properties of the Pane
root.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");
// Add the Canvas to the Pane
root.getChildren().add(canvas);
// Create the Scene
Scene scene = new Scene(root);
// Add the Scene to the Stage
stage.setScene(scene);
// Set the Title of the Stage
stage.setTitle("Drawing a Text on a Canvas");
// Display the Stage
stage.show();
}
}
You can draw text using the fillText() and strokeText() methods of the GraphicsContext using the following snippets of code:
- void strokeText(String text, double x, double y)
- void strokeText(String text, double x, double y, double maxWidth)
- void fillText(String text, double x, double y)
- void fillText(String text, double x, double y, double maxWidth)
Both methods are overloaded. One version lets you specify the text and its position. The other version lets you specify the maximum width of the text as well. If the actual text width exceeds the specified maximum width, the text is resized to fit the specified the maximum width.
The following snippet of code draws a blue filled Text.
// Create the Canvas
Canvas canvas = new Canvas(400, 200);
// Get the graphics context of the canvas
GraphicsContext gc = canvas.getGraphicsContext2D();
// Set line width
gc.setLineWidth(1.0);
// Set fill color
gc.setFill(Color.BLUE);
// Draw a filled Text
gc.fillText("This is a filled Text", 10, 150);
2.3.2 The GUI
The following GUI shows a few examples of stroked and filled texts:

2.4 Drawing Paths
2.4.1 The Code
FxCanvasExample4.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class FxCanvasExample4 extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
@Override
public void start(Stage stage)
{
// Create the Canvas
Canvas canvas = new Canvas(400, 200);
// Get the graphics context of the canvas
GraphicsContext gc = canvas.getGraphicsContext2D();
// Set line width
gc.setLineWidth(2.0);
// Set the Color
gc.setStroke(Color.GREEN);
// Set fill color
gc.setFill(Color.LIGHTCYAN);
// Start the Path
gc.beginPath();
// Make different Paths
gc.moveTo(50, 50);
gc.quadraticCurveTo(30, 150, 300, 200);
gc.fill();
// End the Path
gc.closePath();
// Draw the Path
gc.stroke();
// Create the Pane
Pane root = new Pane();
// Set the Style-properties of the Pane
root.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");
// Add the Canvas to the Pane
root.getChildren().add(canvas);
// Create the Scene
Scene scene = new Scene(root);
// Add the Scene to the Stage
stage.setScene(scene);
// Set the Title of the Stage
stage.setTitle("Drawing Paths on a Canvas");
// Display the Stage
stage.show();
}
}
Use can use path commands and SVG path strings to create a Shape of your choice. A path consists of multiple subpaths. The following methods are used to draw paths:
- beginPath()
- lineTo(double x1, double y1)
- moveTo(double x0, double y0)
- quadraticCurveTo(double xc, double yc, double x1, double y1)
- appendSVGPath(String svgpath)
- arc(double centerX, double centerY, double radiusX, double radiusY, double startAngle, double length)
- arcTo(double x1, double y1, double x2, double y2, double radius)
- bezierCurveTo(double xc1, double yc1, double xc2, double yc2, double x1, double y1)
- closePath()
- stroke()
- fill()
The beginPath() and closePath() methods start and close a path, respectively. Methods such as arcTo() and lineTo() are the path commands to draw a specific type of subpath. Do not forget to call the stroke() or fill() method at the end, which will draw an outline or fill the path.
The following snippet of code draws a quadratic curve. The color of the curve is green and the fill color is lightcyan.
// Start the Path gc.beginPath(); // Make different Paths gc.moveTo(50, 50); gc.quadraticCurveTo(30, 150, 300, 200); gc.fill(); // End the Path gc.closePath();
2.4.2 The GUI
The following image shows a simple example how to draw a path on a canvas:

2.5 Drawing Images
2.5.1 The Code
FxCanvasExample5.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class FxCanvasExample5 extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
@Override
public void start(Stage stage)
{
// Create the Canvas
Canvas canvas = new Canvas(400, 200);
// Get the graphics context of the canvas
GraphicsContext gc = canvas.getGraphicsContext2D();
// Load the Image
String imagePath = "file:\\Path-To-Your-Image\\java-logo.gif";
Image image = new Image(imagePath);
// Draw the Image
gc.drawImage(image, 10, 10, 200, 200);
gc.drawImage(image, 220, 50, 100, 70);
// Create the Pane
Pane root = new Pane();
// Set the Style-properties of the Pane
root.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;");
// Add the Canvas to the Pane
root.getChildren().add(canvas);
// Create the Scene
Scene scene = new Scene(root);
// Add the Scene to the Stage
stage.setScene(scene);
// Set the Title of the Stage
stage.setTitle("Drawing an Image on a Canvas");
// Display the Stage
stage.show();
}
}
You can draw an Download NOW!






valeu! grande trabalho1 (good job!)