0

I'm using javafx for developing an application. My objective is scaling the Pane (which is zooming - although I still have doubts if scaling is same as zooming - please clarify if wrong).

I'm able to scale and translate the pane inside the ScrollPane, but now how can I get its new scaled width and height??

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
                }
            });

        }
    });     

I need this new width and height to resize a canvas and redraw inside it. Please note that in my start of the application, I'm binding some properties.

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

I'm using a ResizableCanvas.java public class ResizableCanvas extends 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;
    }
}
zIronManBox
  • 4,967
  • 6
  • 19
  • 35
  • Buddy you can have a look at this link, as it shows similar scenario http://stackoverflow.com/questions/12394213/scaling-in-javafx-and-scrollpanes – Mudassar Jun 24 '15 at 10:14
  • @Mudassar Not much of Help – zIronManBox Jun 24 '15 at 10:37
  • As of now I'm trying like this: `Platform.runLater(new Runnable() { @Override public void run() { pane.setPrefSize(pane.getWidth()*pane.getScaleX(), pane.getHeight()*pane.getScaleY()); } });` But still not working, for some reason the pane is not resizing .... dont know why? – zIronManBox Jun 24 '15 at 10:38
  • A **complete** example of yours would speed up finding someone who gives you a solution. – Roland Jun 24 '15 at 10:45
  • @Roland Edited the Q, please have a look. My objective is to resize back the canvas as per the Pane and redraw everything in it again. – zIronManBox Jun 24 '15 at 12:25
  • @zIronManBox Sorry, but I don't waste my time to turn these bits and pieces into something that's complete. – Roland Jun 25 '15 at 02:04
  • @Roland what do you mean complete? – zIronManBox Jun 25 '15 at 03:31

0 Answers0