【问题标题】:JavaFX How to center JSON map in a PaneJavaFX 如何在窗格中居中 JSON 映射
【发布时间】:2016-11-30 06:05:16
【问题描述】:

我正在尝试在窗格上显示从 JSON 坐标绘制的用户选择的多个单独地图。现在,地图在左上角绘制。我需要将该地图绘制到窗格的中心,而不是在角落,因此可以正确/普遍地缩放它。如果我使用StackPanesetAlignment(Pos.CENTER);,那么构成地图的所有单独区域(它们是单独绘制的)都在窗格中绘制,而不是整个地图。

这是在窗格上绘制区域的代码:

public void addPolygon(List<Point2D>coordinates, Color color){
    Polygon polygon = new Polygon();
    for(Point2D point : coordinates)
        polygon.getPoints().addAll(point.getX(), point.getY());

        //outline each polygon in black
        polygon.setStroke(Color.BLACK);
        polygon.setStrokeWidth(0.08);

        //sets the color of the polygon derived from a random color generator
        polygon.setFill(color);

    //add created polygons to pane
    mapPane.getChildren().add(polygon);

如何调整它以显示在中心?谢谢!

【问题讨论】:

    标签: json javafx pane


    【解决方案1】:

    我认为解决方案是将各个区域放入一个组中。然后将 Group 放入居中的 StackPane。

    private Group mapGroup = new Group();
    
    public void addPolygon(List<Point2D>coordinates, Color color){
        Polygon polygon = new Polygon();
        for(Point2D point : coordinates)
            polygon.getPoints().addAll(point.getX(), point.getY());
    
            //outline each polygon in black
            polygon.setStroke(Color.BLACK);
            polygon.setStrokeWidth(0.08);
    
            //sets the color of the polygon derived from a random color generator
            polygon.setFill(color);
    
        //add created polygons to pane
        mapGroup.getChildren().add(polygon);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 2019-04-15
      相关资源
      最近更新 更多