Canvas as a root node in Scene Graph?

Viewed 162

I'm new to JavaFX, I'm learning it. I tried to make a canvas object as a root to scene graph in the application. However, I'm not able to do it. I need to create a group or a pane object and add canvas to the one of them. I'm curious about why we are not able to assign a canvas object as a root node. Probably, I'm missing some major points.

The code I tried to compile is as follows;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.canvas.Canvas;
import javafx.scene.Parent;
import javafx.scene.Scene;

public class driver extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    Canvas canvas;

    @Override
    public void start( Stage stage ) {
        canvas = new Canvas(300, 300);
        Scene scene = new Scene(canvas, 300, 300);
        stage.setScene(scene);
        stage.show();
    }
}
1 Answers

You can't use Canvas as the parameter to the constructor(s) for Scene, because Scene takes a Parent as parameter. Canvas has superclass Node, not Parent. To verify, just check the Javadocs for both of them.

Related