So I am trying to make a scene that is a pane containing a canvas. This canvas will need to be inside this pane as I am going to display information along side it. I have used scenebuilder to create a pane then placed a canavas called "drawing" inside of it. I then load the scene and attempt to then draw in the canvas however I get and error saying the canvas is null. I know there are answers to similar questions however they do not answer my question in my context I don't feel. If they do could you please explain how as I don't understand.
package uk.ac.rhul.cs3821;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.stage.Stage;
public class TurtleView extends Application {
private static volatile TurtleView instance = null;
Turtle turtle = new Turtle(0,0,0);
@FXML
private Canvas drawing;
private GraphicsContext gc;
@FXML
void initialize() {
instance = this;
}
/**
* Creates a JavaFX instance and Launches the JavaFX application.
*
* @return the JavaFX instance running
*/
public static synchronized TurtleView getInstance() {
if (instance == null) {
new Thread(() -> Application.launch(TurtleView.class)).start();
// Wait until the instance is ready since initialise has executed.
while (instance == null) {// empty body
}
}
return instance;
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(RuleView.class.getResource("TurtleView.fxml"));
Scene scene = new Scene(root, 800, 500);
primaryStage.setScene(scene);
primaryStage.show();
gc = drawing.getGraphicsContext2D();
}
}