如何在 JavaFX 形状中插入 HTML 文本?

How to insert HTML text inside a JavaFX shape?

我想在JavaFx中创建一个基于树的算法可视化,并且符号中有很多子脚本和超级脚本。我想将这些符号添加到圆形等形状中。

我曾尝试使用 WebView 对象来执行此操作,但它只是覆盖了整个屏幕。

public void start(Stage primaryStage) throws Exception{

        primaryStage.setTitle("Shape Text");
        Group circles = new Group();
        Circle circle = new Circle(50, Color.web("white", 0.7));
        circle.setCenterX(500.0f);
        circle.setCenterY(200.0f);
        circle.setStrokeType(StrokeType.OUTSIDE);
        circle.setStroke(Color.web("white", 0.16));
        circle.setStrokeWidth(4);
        circles.getChildren().add(circle);

        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.loadContent("<h1>B<sub>0</sub></h1>");

        StackPane stack = new StackPane();
        stack.getChildren().addAll(circles, webView);

        Scene scene = new Scene(stack, 1000, 800, Color.BLACK);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

以上代码将整个视图替换为 HTML 文本。我也试过javafx.scene.text.Textclass,但是不支持HTML的内容。

提前致谢!

尝试设置 WebViewmaxSize

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class JavaFXTestingGround extends Application
{

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        Circle circle = new Circle(100, Color.web("white", 0.7));

        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.loadContent("<h1>B<sub>0</sub></h1>");
        webView.setMaxSize(50, 50);

        StackPane stack = new StackPane();
        stack.getChildren().addAll(circle, webView);

        Scene scene = new Scene(stack, 1000, 800, Color.BLACK);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

您可能需要做三件事:

  1. Size the WebView to the HTML content(或形状的内部显示区域)。
  2. 制作 background of the WebView pages transparent.
  3. Center the HTML content in the WebView,WebView 在形状中居中。

下面的代码演示了其中的一些技巧:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.StrokeType;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

import java.lang.reflect.Field;

public class ShapedHTML extends Application {
    @Override
    public void start(Stage stage) throws Exception{
        stage.setTitle("Shape Text");
        Group circles = new Group();
        Circle circle = new Circle(50, Color.web("white", 0.7));
        circle.setCenterX(500.0f);
        circle.setCenterY(200.0f);
        circle.setStrokeType(StrokeType.OUTSIDE);
        circle.setStroke(Color.web("white", 0.16));
        circle.setStrokeWidth(4);
        circles.getChildren().add(circle);

        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webView.maxWidthProperty().bind(circle.radiusProperty().multiply(2));
        webView.maxHeightProperty().bind(circle.radiusProperty().multiply(2));
        webEngine.documentProperty().addListener(observable -> {                   
            try {
                // Use reflection to retrieve the WebEngine's private 'page' field.
                Field f = webEngine.getClass().getDeclaredField("page");
                f.setAccessible(true);
                com.sun.webkit.WebPage page = (com.sun.webkit.WebPage) f.get(webEngine);
                page.setBackgroundColor((new java.awt.Color(0, 0, 0, 0)).getRGB());
            } catch (Exception e) {
                System.out.println("Difficulty to make WebView background transparent");
                e.printStackTrace();
            }
        });

        webEngine.loadContent("<h1 id='root' style='background : rgba(0,0,0,0); margin: 0; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);'>B<sub>0</sub></h1>");

        StackPane stack = new StackPane();
        stack.getChildren().addAll(circles, webView);

        Scene scene = new Scene(stack, 1000, 800, Color.BLACK);
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }

}

com.sunclass用法注意事项

以上代码使用 com.sun classes(通常不推荐,因为它没有公开支持 API)。但是,它对我有用(在 Java 8 上),我不知道有什么更好的方法来实现 WebView 背景的透明度。

如果您使用的是更高版本的 Java(例如 Java 11+),那么您将需要提供一些 VM 参数以允许使用相关的 com.sun class 上班。例如,请参阅堆栈溢出问题 以解决 Java 11+ 中 com.sun.webkit.WebPage 的可访问性问题。该问题的答案建议使用以下 VM 参数(我还没有尝试过):

--add-exports javafx.web/com.sun.webkit=projectname

其中最后一个参数是您在项目的 module-info.java 中声明的模块名称。