1

I am drawing a maze on canvas. i want to allow the user to zoom in and out of the maze by pressing the CTRL button and the mouse mouse rotate button. but i need the zoom to be based on the mouse location.

my code works but it makes the zoom always on the top left corner of the maze and not based on the mouse location.

how can i make the zoom in/out action be based on the mouse location?

in my FXML file i declared:

onScroll="#Zoom"

in my main control class i declared the zoom function:

    public void Zoom(ScrollEvent scrollEvent) {
        mazeDisplayer.zoom(scrollEvent);
    }

which calls the MazeDisplayer class which extends the canvas:

package View;

import algorithms.mazeGenerators.Maze;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Alert;
import javafx.scene.image.Image;
import javafx.scene.input.ScrollEvent;
import javafx.scene.paint.Color;

import java.awt.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class  MazeDisplayer extends Canvas {

    private Maze m;
    private int row_player;
    private int col_player;
    private SolDisplayer solDisplayer;
    private double for_zoom =1.05;
  //  public static int d=0;
    StringProperty imageFileNameWall = new SimpleStringProperty();
    StringProperty imageFileNamePlayer = new SimpleStringProperty();
    StringProperty imageFileNameEnd = new SimpleStringProperty();





    public int getRow_player() {
        return row_player;
    }

    public int getCol_player() {
        return col_player;
    }




    public void set_player_position(int row, int col){
        this.row_player = row;
        this.col_player = col;
        draw();


    }

    public void set_player_position_without_draw(int row, int col){
        this.row_player = row;
        this.col_player = col;

        }
    public void drawMaze(Maze maze)
    {
        this.m = maze;
        draw();
    }

    public void draw()
    {
        if( m!=null)
        {
            double canvasHeight = getHeight();
            double canvasWidth = getWidth();
            int row = m.getNumOfRows();
            int col = m.getNumOfCols();
            double cellHeight = canvasHeight/row;
            double cellWidth = canvasWidth/col;
            GraphicsContext graphicsContext = getGraphicsContext2D();
            graphicsContext.clearRect(0,0,canvasWidth,canvasHeight);
            graphicsContext.setFill(Color.CHOCOLATE);
            double w,h;
            int [][] matrix=m.getMatrix();
            //Draw Maze
            Image wallImage = null;
            try {
                wallImage = new Image(new FileInputStream(getImageFileNameWall()));
            } catch (FileNotFoundException e) {
                System.out.println("There is no file....");
            }
            for(int i=0;i<row;i++)
            {
                for(int j=0;j<col;j++)
                {
                    if(matrix[i][j] == 1) // Wall
                    {
                        h = i * cellHeight;
                        w = j * cellWidth;
                        if (wallImage == null){
                            graphicsContext.fillRect(w,h,cellWidth,cellHeight);
                        }else{
                            graphicsContext.drawImage(wallImage,w,h,cellWidth,cellHeight);
                        }
                    }

                }
            }
            {
                double h_player=this.getRow_player() * cellHeight;
                double w_player=this.getCol_player() * cellWidth;
                Image playerImage = null;
                try {
                    playerImage = new Image(new FileInputStream(getImageFileNamePlayer()));
                } catch (FileNotFoundException e) {
                    System.out.println("There is no Image player....");
                }
                graphicsContext.drawImage(playerImage,w_player,h_player,cellWidth,cellHeight);
            }

            double h_end = this.m.getGoalPosition().getRowIndex() * cellHeight;
            double w_end = this.m.getGoalPosition().getColumnIndex() * cellWidth;
            Image endImage = null;
            try {
                endImage = new Image(new FileInputStream(getImageFileNameEnd()));
            } catch (FileNotFoundException e) {
                System.out.println("There is no Image player....");
            }
            graphicsContext.drawImage(endImage,w_end,h_end,cellWidth,cellHeight);
        }
    }

    public void zoom (ScrollEvent scrollEvent){
        if(scrollEvent.isControlDown()) {
            if (scrollEvent.getDeltaY() < 0)
            {
                setHeight(getHeight() / for_zoom);
                setWidth(getWidth() / for_zoom);
                draw();
                }
            }
            if (scrollEvent.getDeltaY() > 0) {
                if(getWidth()>6300 || getHeight()>6300)
                    setHeight(getHeight()*for_zoom);
                    setWidth(getWidth() * for_zoom);
                    draw();
                }
            }
        }
    }

}

0 Answers0