Class Canvas

java.lang.Object
javafx.scene.Node
javafx.scene.canvas.Canvas
All Implemented Interfaces:
Styleable, EventTarget

public non-sealed class Canvas extends Node
Canvas is an image that can be drawn on using a set of graphics commands provided by a GraphicsContext.

A Canvas node is constructed with a width and height that specifies the size of the image into which the canvas drawing commands are rendered. All drawing operations are clipped to the bounds of that image.

Example:

import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.canvas.*;

Group root = new Group();
Scene s = new Scene(root, 300, 300, Color.BLACK);

final Canvas canvas = new Canvas(250,250);
GraphicsContext gc = canvas.getGraphicsContext2D();

gc.setFill(Color.BLUE);
gc.fillRect(75,75,100,100);

root.getChildren().add(canvas);
 
Since:
JavaFX 2.2
See Also:
  • Property Details

  • Constructor Details

    • Canvas

      public Canvas()
      Creates an empty instance of Canvas.
    • Canvas

      public Canvas(double width, double height)
      Creates a new instance of Canvas with the given size.
      Parameters:
      width - width of the canvas
      height - height of the canvas
  • Method Details

    • getGraphicsContext2D

      public GraphicsContext getGraphicsContext2D()
      returns the GraphicsContext associated with this Canvas.
      Returns:
      the GraphicsContext associated with this Canvas
    • setWidth

      public final void setWidth(double value)
      Sets the value of the width property.
      Property description:
      Defines the width of the canvas.
      Default value:
      0.0
      Parameters:
      value - the value for the width property
      See Also:
    • getWidth

      public final double getWidth()
      Gets the value of the width property.
      Property description:
      Defines the width of the canvas.
      Default value:
      0.0
      Returns:
      the value of the width property
      See Also:
    • widthProperty

      public final DoubleProperty widthProperty()
      Defines the width of the canvas.
      Default value:
      0.0
      Returns:
      the width property
      See Also:
    • setHeight

      public final void setHeight(double value)
      Sets the value of the height property.
      Property description:
      Defines the height of the canvas.
      Default value:
      0.0
      Parameters:
      value - the value for the height property
      See Also:
    • getHeight

      public final double getHeight()
      Gets the value of the height property.
      Property description:
      Defines the height of the canvas.
      Default value:
      0.0
      Returns:
      the value of the height property
      See Also:
    • heightProperty

      public final DoubleProperty heightProperty()
      Defines the height of the canvas.
      Default value:
      0.0
      Returns:
      the height property
      See Also: