拍摄JavaFX TextArea和WebView的快照

时间:2017-02-15 16:54:11

标签: javafx webview textarea snapshot

我有以下问题: 我正在编写一个类似于空白纸的程序,您可以在其上书写(免费手写),插入文本,添加图像,添加PDF等... 对于一个特定功能,我需要将用户添加到窗格的节点转换为图像。值得庆幸的是,JavaFX-Nodes提供了一个很好的方法:

public void snapshot(...)

但是有一个问题:当我试图制作文本对象的快照时,它们会失败。我可以拍摄快照的唯一节点是javafx.scene.text.Text。 以下类失败:

javafx.scene.control.TextArea
javafx.scene.web.WebView

这是一个说明我的问题的例子:

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {

            TextArea textArea = new TextArea("Lorem Ipsum is simply dummy text"
                    + " of the printing and typesetting industry. Lorem Ipsum has been \n"
                    + "the industry's standard dummy text ever since the 1500s, when an \n"
                    + "unknown printer took a galley of type and scrambled it to make a type\n"
                    + " specimen book. It has survived not only five centuries, but also the\n"
                    + " leap into electronic typesetting, remaining essentially unchanged. It\n"
                    + " was popularised in the 1960s with the release of Letraset sheets containing\n"
                    + " Lorem Ipsum passages, and more recently with desktop publishing software \n"
                    + "like Aldus PageMaker including versions of Lorem Ipsum");

            SnapshotParameters snapshotParameters = new SnapshotParameters();
            snapshotParameters.setFill(Color.TRANSPARENT);

            Image img = textArea.snapshot(snapshotParameters, null);
            ImageView imgVw = new ImageView( img );

            System.out.printf("img.width: %s    height: %s%n", img.getWidth(), img.getHeight()); // <= width and height of the image img is 1:1! WHY?

            Pane pane = new Pane();
            pane.getChildren().addAll(imgVw);

            Scene scene = new Scene(pane, 800,800);

            pane.setMinWidth(800);
            pane.setMinHeight(800);
            pane.setMaxWidth(800);
            pane.setMaxHeight(800);

            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }



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

我可以通过创建一个javafx.scene.text.Text-Object来考虑解决方法并拍摄快照。但是,对于javafx.scene.web.WebView显示的格式化文本,这将失败。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

在对TextArea进行快照之前,Scene必须是snapshot javadoc。在快照调用之前将以下行添加到代码中,代码将按预期工作:

Scene snapshotScene = new Scene(textArea);

{{3}}中提到了此要求:

  

注意:为了使CSS和布局正常运行,节点必须   成为场景的一部分(场景可能附加到舞台,但不需要   定)。