【问题标题】:JavaFX Scaling differently sized nodes to the same sizeJavaFX将不同大小的节点缩放到相同的大小
【发布时间】:2016-12-04 00:45:24
【问题描述】:

我正在窗格上绘制不同大小的地图。有些看起来不错,有些只是呈现为一个小形状,您必须放大才能使其大小合适。我希望每次初始化时这些地图的大小大致相同(因此我不必手动缩放每张地图)。我有minmaxxy 值的Point2D 点,它们绘制在窗格上,地图也是如此(这是多边形的Group) .如何设置PaneminPointGroupminPoint 之间的距离?还是我以错误的方式处理这个问题?

编辑:

 public void setDistance(Group map, Point2D paneSize, Point2D mapSize){
    //um diese distance verschieben, if distance > 10px (scale)
    double d = paneSize.distance(mapSize);
    double scale = ??
    map.setScaleX(scale);
    map.setScaleY(scale);
}

这就是我打算这样做的方式,但不确定那一行。

【问题讨论】:

    标签: javafx scaling


    【解决方案1】:

    要将节点缩放到父节点的大小,大小的差异并不重要。重要的是大小的商,更准确地说是高度和宽度的商的最小值(假设您想完全填充一个方向的父级)。

    例子:

    @Override
    public void start(Stage primaryStage) {
        Text text = new Text("Hello World!");
    
        Pane root = new Pane();
        root.getChildren().add(text);
        InvalidationListener listener = o -> {
            Bounds rootBounds = root.getLayoutBounds();
            Bounds elementBounds = text.getLayoutBounds();
    
            double scale = Math.min(rootBounds.getWidth() / elementBounds.getWidth(),
                    rootBounds.getHeight() / elementBounds.getHeight());
            text.setScaleX(scale);
            text.setScaleY(scale);
    
            // center the element
            elementBounds = text.getBoundsInParent();
            double cx = (elementBounds.getMinX() + elementBounds.getMaxX()) / 2;
            double cy = (elementBounds.getMinY() + elementBounds.getMaxY()) / 2;
            text.setTranslateX(rootBounds.getWidth() / 2 - cx + text.getTranslateX());
            text.setTranslateY(rootBounds.getHeight() / 2 - cy + text.getTranslateY());
        };
    
        root.layoutBoundsProperty().addListener(listener);
        text.layoutBoundsProperty().addListener(listener);
    
        Scene scene = new Scene(root);
    
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 2016-09-15
      • 1970-01-01
      • 2014-08-19
      相关资源
      最近更新 更多