Scaling in JavaFX and ScrollPanes

前端 未结 2 1185
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 03:41

I\'ve been trying to work with the scaling transform in JavaFX, but haven\'t quite been able to wrap my head around it. Basically, I have a Pane containing a complex graph a

相关标签:
2条回答
  • 2020-12-19 04:15

    According to the ScrollPane document you might try to wrap a Pane in a Group so the ScrollPane is scroll by visual bound not the actual layout bound.

    ScrollPane layout calculations are based on the layoutBounds rather than the
    boundsInParent (visual bounds) of the scroll node. If an application wants the
    scrolling to be based on the visual bounds of the node (for scaled content etc.),
    they need to wrap the scroll node in a Group. 
    
    0 讨论(0)
  • 2020-12-19 04:23

    I implemented scaling in a ScrollPane for Graphs and other nodes in this example of scrollpane viewports, transforms and layout bounds in JavaFX.

    The code was implemented when I was first learning JavaFX, so certainly the code could be cleaner and perhaps there are simpler ways to accomplish this (e.g. using a Group as the container for the scaled node as suggested in the ScrollPane documentation).

    One key to getting the solution I wanted (ScrollBars only appearing when you are zoomed in and the node is larger than the visible viewport), was this code:

    // create a container for the viewable node.
    final StackPane nodeContainer = new StackPane();
    nodeContainer.getChildren().add(node);
    
    // place the container in the scrollpane and adjust the pane's viewports as required.
    final ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(nodeContainer);
    scrollPane.viewportBoundsProperty().addListener(
      new ChangeListener<Bounds>() {
      @Override public void changed(ObservableValue<? extends Bounds> observableValue, Bounds oldBounds, Bounds newBounds) {
        nodeContainer.setPrefSize(
          Math.max(node.getBoundsInParent().getMaxX(), newBounds.getWidth()),
          Math.max(node.getBoundsInParent().getMaxY(), newBounds.getHeight())
        );
      }
    });
    
    ...
    
    // adjust the view layout based on the node scalefactor.
    final ToggleButton scale = new ToggleButton("Scale");
    scale.setOnAction(new EventHandler<ActionEvent>() {
      @Override public void handle(ActionEvent actionEvent) {
        if (scale.isSelected()) {
          node.setScaleX(3); node.setScaleY(3);
        } else {
          node.setScaleX(1); node.setScaleY(1);
        }
        // runlater as we want to size the container after a layout pass has been performed on the scaled node.
        Platform.runLater(new Runnable() { 
          @Override public void run() {
            nodeContainer.setPrefSize(
              Math.max(nodeContainer.getBoundsInParent().getMaxX(), scrollPane.getViewportBounds().getWidth()),
              Math.max(nodeContainer.getBoundsInParent().getMaxY(), scrollPane.getViewportBounds().getHeight())
            );
          }
        });
      }
    });
    
    0 讨论(0)
提交回复
热议问题