2016-12-04 78 views
0

我在一个窗格上绘制不同大小的地图。有些看起来体面,其他人只是呈现一个小形状,你必须放大以获得它的大小。我希望这些地图在每次初始化时都显示大致相同的尺寸(所以我不必手动缩放每个地图)。我有Point2D积分minmax值分别为xy他们被绘制在窗格上,同样适用于地图(这是多边形的Group)。如何设置minPointPaneminPointGroup之间的距离?还是我以错误的方式接近这个?JavaFX将不同大小的节点缩放到相同的大小

编辑:

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); 
} 

这就是我打算做下去,不知道尽管这一条线。

回答

0

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

例子:

@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(); 
} 
+0

谢谢你,那个作品! – dot

相关问题