【问题标题】:Drawing only the visible portion of a Canvas in a scrollPane JavaFX在 scrollPane JavaFX 中仅绘制 Canvas 的可见部分
【发布时间】:2016-05-21 13:30:27
【问题描述】:

如果重要的话,我会使用 SceneBuilder。

老实说,似乎无法弄清楚。我不明白如何才能访问 scrollPane 的可见部分,或者通过其他方式确定我需要的边界。 scrollPane 为 300x300,而画布则更大。我只想在画布上绘制一个 300x300 像素的部分。

【问题讨论】:

    标签: user-interface canvas javafx scrollpane


    【解决方案1】:

    ScrollPane 有一个 ViewPort。使用 ViewPort 的边界。

    代码

    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.geometry.Bounds;
    import javafx.scene.Scene;
    import javafx.scene.canvas.Canvas;
    import javafx.scene.canvas.GraphicsContext;
    import javafx.scene.control.ScrollPane;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
        Canvas canvas = new Canvas( 2000, 2000);
    
        @Override
        public void start(Stage primaryStage) {
    
            ScrollPane scrollPane = new ScrollPane( canvas);
    
            scrollPane.viewportBoundsProperty().addListener((ChangeListener<Bounds>) (observable, oldValue, newValue) -> showBounds( scrollPane));
            scrollPane.hvalueProperty().addListener((ChangeListener<Number>) (observable, oldValue, newValue) -> showBounds( scrollPane));
            scrollPane.vvalueProperty().addListener((ChangeListener<Number>) (observable, oldValue, newValue) -> showBounds( scrollPane));
    
            Scene scene = new Scene( scrollPane, 400, 400);
    
            primaryStage.setScene( scene);
            primaryStage.show();
    
        }
    
        private void showBounds( ScrollPane scrollPane) {
    
            double hValue = scrollPane.getHvalue();
            double vValue = scrollPane.getVvalue();
            double width = scrollPane.viewportBoundsProperty().get().getWidth();
            double height = scrollPane.viewportBoundsProperty().get().getHeight();
    
            double x = (scrollPane.getContent().getBoundsInParent().getWidth() - width) * hValue;
            double y = (scrollPane.getContent().getBoundsInParent().getHeight() - height) * vValue;
    
            System.out.println( "x=" + x + ", y=" + y + ", width=" + width + ", height=" + height);
    
            // demo: draw a line of the canvas size and a rectangle of the viewport size => the rectangle must always be in the center
            double size = 80;
            GraphicsContext gc = canvas.getGraphicsContext2D();
            gc.clearRect(x, y, width, height);
    
            gc.beginPath();
            gc.moveTo(0, 0);
            gc.lineTo(canvas.getWidth(), canvas.getHeight());
            gc.closePath();
            gc.stroke();
    
            gc.fillRect(x + (width-size) / 2, y + (height-size) / 2, size, size);
    
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多