我正在窗格上绘制大小不同的地图。有些看起来不错,其他的只是呈现为小形状,您必须放大才能将其缩放到合适的大小。我希望这些地图在每次初始化时都显示为大致相同的大小(因此,我不必手动缩放每个地图)。我已经得到Point2D
的点min
和max
值x
和y
他们绘制上同样适用于地图(这是一个窗格的Group
多边形)。如何设置minPoint
ofPane
与minPoint
of之间的距离Group
?还是我走错路了?
编辑:
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);
}
这就是我计划这样做的方式,尽管不确定那一行。
为了将节点缩放到父节点的大小,大小的差异并不重要。重要的是尺寸的商,更确切地说是高度和宽度的商的最小值(假设您要完全沿一个方向填充父级)。
例子:
@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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句