【发布时间】:2021-12-12 06:29:10
【问题描述】:
所以我正在尝试制作一个包含画布的窗格的场景。这个画布需要在这个窗格内,因为我将在它旁边显示信息。我使用scenebuilder创建了一个窗格,然后在其中放置了一个名为“绘图”的画布。然后我加载场景并尝试在画布中绘制,但是我得到并且错误说画布为空。我知道有类似问题的答案,但是在我不觉得的情况下,他们没有回答我的问题。如果他们这样做,您能否解释一下,因为我不明白。
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();
}
}
【问题讨论】: