如何在JavaFX中修复字体较小的扭曲文本?

时间:2019-01-28 21:23:24

标签: java javafx text scale

我正在使用JavaFX构建一个小型应用程序以可视化电路板。该应用程序包括缩放和移动的可能性。当我显示小文本时,它们会变形,如下面的屏幕截图所示。只有当我将场景放大到足够远时,文本才能再次正确显示。有谁能解决这个问题?我提供了截屏的示例代码。

@Override
public void start(Stage primaryStage) {
    Pane root = new Pane();
    addMouseEventHandler(root);

    Pane pane1 = new Pane();
    Text text1 = new Text(400, 400, "Text 1");
    text1.setFont(new Font(5));
    pane1.getChildren().addAll(text1);
    root.getChildren().add(pane1);

    primaryStage.setTitle("Text scaling problem");
    primaryStage.setScene(new Scene(root, 800, 800));
    primaryStage.show();
}

private void addMouseEventHandler(Pane root) {
    // add scroll handling to zoom in and out
    root.setOnScroll((event) ->
    {
        double factor = event.getDeltaY() > 0 ? 1.1 : 0.9;
        root.setScaleX(root.getScaleX() * factor);
        root.setScaleY(root.getScaleY() * factor);
    });

    // add drag handling
    root.setOnMousePressed((mouseEvent) ->
    {
        startMouseX = mouseEvent.getSceneX();
        startMouseY = mouseEvent.getSceneY();

        startTranslationX = root.getTranslateX();
        startTranslationY = root.getTranslateY();
    });

    root.setOnMouseDragged((mouseEvent) ->
    {
        double movedX = startMouseX - mouseEvent.getSceneX();
        double movedY = startMouseY - mouseEvent.getSceneY();

        double transX = startTranslationX - movedX;
        double transY = startTranslationY - movedY;

        root.setTranslateX(transX);
        root.setTranslateY(transY);
    });
}

correct textdistorted text

正确的文本比变形的文本滚动了一步。

1 个答案:

答案 0 :(得分:0)

我不确定是否可以对文本节点内的文本缩放进行任何操作。但是如果将字符分隔在不同的文本节点中,它将保持其形状。您可能想尝试使用Canvas并使用GraphicsContext2D手动绘制。

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class DistortedText extends Application{

    private double startMouseX, startMouseY;
    private double startTranslationX, startTranslationY;

    @Override
    public void start(Stage primaryStage) {
        HBox box = new HBox(new Text("T"),new Text("e"),new Text("x"),new Text("T"),new Text(" "),new Text("1"));

        Pane root = new Pane(box);
        addMouseEventHandler(root,box);

        primaryStage.setTitle("Text scaling problem");
        primaryStage.setScene(new Scene(root, 800, 800));
        primaryStage.show();
    }

    private void addMouseEventHandler( Pane container, Node node) {
        // add scroll handling to zoom in and out
        container.setOnScroll((event) ->
        {
            double factor = event.getDeltaY() > 0 ? 1.1 : 0.9;
            node.setScaleX(node.getScaleX() * factor);
            node.setScaleY(node.getScaleY() * factor);
        });

        // add drag handling
        container.setOnMousePressed((mouseEvent) ->
        {
            startMouseX = mouseEvent.getX();
            startMouseY = mouseEvent.getY();

            startTranslationX = node.getTranslateX();
            startTranslationY = node.getTranslateY();
        });

        container.setOnMouseDragged((mouseEvent) ->
        {
            double movedX = startMouseX - mouseEvent.getX();
            double movedY = startMouseY - mouseEvent.getY();

            double transX = startTranslationX - movedX;
            double transY = startTranslationY - movedY;

            node.setTranslateX(transX);
            node.setTranslateY(transY);
        });
    }

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