javafx获取缩放窗格的宽度和高度以调整画布并在画布内重绘

时间:2015-06-24 10:02:24

标签: java javafx javafx-2

我正在使用javafx开发应用程序。我的目标是缩放窗格(缩放 - 虽然我仍然怀疑缩放是否与缩放相同 - 请澄清是否错误)。

我能够缩放和翻译ScrollPane中的窗格,但现在我如何才能获得新的缩放宽度和高度?

slider.valueProperty().addListener(new ChangeListener<Number>() {

        @Override
        public void changed(ObservableValue<? extends Number> observable,
                Number oldValue, Number newValue) {

            pane.setScaleX(newValue.doubleValue());
            pane.setScaleY(newValue.doubleValue());

            pane.setOnMousePressed(new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {
                      pressedX = event.getX();
                      pressedY = event.getY();
                    }
            });

            pane.setOnMouseDragged(new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {
                    pane.setTranslateX(pane.getTranslateX() + event.getX() - pressedX);
                    pane.setTranslateY(pane.getTranslateY() + event.getY() - pressedY);
                    event.consume();

                }
            });

            if(newValue.intValue()==1) {
                pane.setScaleX(1.0);
                pane.setScaleY(1.0);
                pane.setTranslateX(0.0);
                pane.setTranslateY(0.0);
                pane.setTranslateZ(0.0);
            }

            Platform.runLater(new Runnable() { 

                @Override 
                public void run() {
                    scrollPane.setPrefWidth(pane.getWidth()*pane.getScaleX());
                    scrollPane.setPrefHeight(pane.getHeight()*pane.getScaleY());
                    pane.resize(scrollPane.getPrefWidth(),
                            scrollPane.getPrefHeight());//Here once pane is resized, ResizableCanvas also draws

                    for(int i=0;i<canvas.length;i++){
                        canvas[i].setScaledWidth(scrollPane.getPrefWidth());
                        canvas[i].setScaledHeight(scrollPane.getPrefHeight());
                    }

                    mChart.redrawAllAgain();//redraw whole chart again with resized canvas
                }
            });

        }
    });     

我需要这个新的宽度和高度来调整画布大小并在其中重绘。 请注意,在我的应用程序开始时,我绑定了一些属性。

resizableCanvas1.widthProperty().bind(pane.widthProperty());
resizableCanvas1.heightProperty().bind(pane.heightProperty());

我正在使用 ResizableCanvas.java     公共类ResizableCanvas扩展了Canvas {

    private double scaledWidth;
    private double scaledHeight;
    private boolean once = true;

    public ResizableCanvas() {
        // Redraw canvas when size changes.
        widthProperty().addListener(evt -> draw());
        heightProperty().addListener(evt -> draw());
        System.out.println("Canvas: "+scaledWidth+","+scaledHeight);
    }

    private void draw() {
        double width = getWidth();
        double height = getHeight();
        if(once) {
            scaledWidth = width;
            scaledHeight = height;
        }
        GraphicsContext gc = getGraphicsContext2D();
        gc.clearRect(0, 0, width, height);
        gc.setFill(Color.BLACK);
        gc.fillRect(0,0,width, height);
        gc.setStroke(Color.RED);
        gc.strokeLine(0, 0, width, height);
        gc.strokeLine(0, height, width, 0);
    }

    @Override
    public boolean isResizable() {
        return true;
    }

    @Override
    public double prefWidth(double height) {
        return getWidth();
    }

    @Override
    public double prefHeight(double width) {
        return getHeight();
    }

    public void setScaledDimension(boolean is) {
        this.once = is;
    }

    public void setScaledWidth(double sWidth) {
        this.scaledWidth = sWidth;
    }

    public void setScaledHeight(double sHeight) {
        this.scaledHeight = sHeight;
    }

    public double getScaledWidth() {
        return this.scaledWidth;
    }

    public double getScaledHeight() {
        return this.scaledHeight;
    }
}

0 个答案:

没有答案