简体   繁体   English

使用和更新Canvas节点的JavaFX 8.0 Scene Builder 2.0

[英]JavaFX 8.0 Scene Builder 2.0 using and updating a Canvas Node

so i am using SceneBuilder 2.0 to create a stage with a Canvas node and then use the ContextGraphics of the Canvas to modify it from the controller file, is this possible? 所以我正在使用SceneBuilder 2.0创建一个具有Canvas节点的舞台,然后使用Canvas的ContextGraphics从控制器文件中对其进行修改,这可能吗? i read that the Canvas can be modified before it is attached to a Stage from any thread, but if its already been attached to a stage it has to be modified by the JavaFX application thread or else you will find problems like the Canvas not updating (which is exactly what is happening to me). 我读到Canvas可以在从任何线程连接到Stage之前进行修改,但是如果它已经连接到Stage,则必须由JavaFX应用程序线程对其进行修改,否则您会发现Canvas无法更新的问题(这正是我正在发生的事情。

I currently have this code which is not working: 我目前有此代码无法正常工作:

in the JavaFX Application: 在JavaFX应用程序中:

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("Window.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

And in the controller file: 并在控制器文件中:

@FXML
private Canvas canvas;

public void render() {
    GraphicsContext gc = canvas.getGraphicsContext2D();
    Image image1 = new Image("/img/Test1.png");
    gc.drawImage(image1, 300, 300);
}

and then i call the method render() but the Canvas wont update, any ideas? 然后我调用方法render()但Canvas不会更新,有什么想法吗?

You are not telling us where and from which thread you are calling the render method. 您没有告诉我们您在哪里以及从哪个线程调用render方法。 Anyway - I'd bet that your image has just not been loaded correctly. 无论如何-我敢打赌,您的图片尚未正确加载。 Check the error property of the image before you draw it. 在绘制图像之前,请检查图像的错误属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM