Q:

How can I interact with my canvas in the FXMLDocumentController

I want to draw pixels on my canvas after pushing a button (so in the handleButtonAction function of my FXMLDocumentController)
public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;
    private Canvas canvas;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        //I'd like to perform the interaction with the Canvas here
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

but everywhere I looked I only saw examples where the canvas was changed in the start function like this:
public class BasicOpsTest extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Drawing Operations Test");
        Group root = new Group();
        Canvas canvas = new Canvas(300, 250);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        drawShapes(gc);
        root.getChildren().add(canvas);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    private void drawShapes(GraphicsContext gc) {
        gc.setFill(Color.GREEN);
        gc.setStroke(Color.BLUE);
        gc.setLineWidth(5);
        gc.strokeLine(40, 10, 10, 40);
    }
}

is there a way to do that?

A:

Why don't you just try it? You can manipulate the canvas at any time. This is neither bound to the start method nor the initialize method. But I guess you will have to add an extra @FXML annotation to your canvas element to get that working.

