如何在其他线程中对 JavaFX 进行快照

问题描述 投票:0回答:0

因为 java 现在没有将 css3 html 转换为图像的库,我必须尝试使用 fxml 生成图像,该服务实际上接收用户请求并生成图像,但是当我直接加载 fxml 并截取屏幕截图时,它必须运行在 FX 线程上:

java.lang.IllegalStateException: Not on FX application thread; currentThread = main

这是代码:

        Parent root = FXMLLoader.load(new File("form.fxml").toURI().toURL());
        Scene scene = new Scene(root);
        WritableImage image = scene.snapshot(null);
        ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", new File("Test.png"));

如果我使用类扩展应用程序

public class Test extends Application 

和开始时的快照

@Override
public void start(Stage primaryStage) throws Exception
{
    Parent root = FXMLLoader.load(new File("bedwars.fxml").toURI().toURL());
    Scene scene = new Scene(root);
    WritableImage image = scene.snapshot(null);
    ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", new File("Test.png"));
    Platform.exit();
}

使用launch()

起初它产生了,但是当我再次开始时

Caused by: java.lang.IllegalStateException: Application launch must not be called more than once

所以有一些方法可以重复生成屏幕截图吗?或任何呈现 css3 html 和 fxml 库? 谢谢!

java html javafx awt fxml
© www.soinside.com 2019 - 2024. All rights reserved.