我怎么知道javafx中一个单元格的位置(x,y)?

时间:2017-09-05 02:43:51

标签: javafx position draw

我在JavaFX中做迷宫,我需要从起点到家画一条​​线(Bresenham线),我需要知道两个单元的位置但是我知道如何获得位置,我已经使用getLayout但它总是返回0.0

这是我的控制器代码:

public void init(float numero){
    nume_obst = numero;
    //generate the maze
    for (int y = 0; y < Y_TILES; y++){
        for (int x=0; x < X_TILES; x++){
            Tiles tiles = new Tiles(x,y,Math.random() < nume_obst);
            grid[x][y] = tiles;
            pnMaze.getChildren().add(tiles);
        }
    }
}

public void setBtn(){
    if (btnHome.isFocused()){
        home = true;
        btnHome.setDisable(true);
    }

    if (btnPlayer.isFocused()){
        player = true;
        btnPlayer.setDisable(true);
    }

    if (btnStart.isFocused()){
        if (!btnHome.isDisable() && !btnPlayer.isDisable()){
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Error");
            alert.setContentText("No puedes iniciar el juego si no has puesto la meta y el inicio del jugador");

            alert.show();
        }else{

        }
    }
}

这是绘制每个单元格的类tile的代码

 public class Tiles extends StackPane {
    private int x,y;
    private boolean obstacle;
    private  Label mountain = new Label();
    private double homeX,homeY;

    private Rectangle border = new Rectangle(TILE_SIZE - 2,TILE_SIZE - 2);

    public Tiles(int x,int y,boolean obstacle){
        this.x = x;
        this.y = y;
        this.obstacle = obstacle;



        border.setStroke(Color.BLACK);
        border.setFill(Color.WHITE);

        mountain.setGraphic(obstacle ? new ImageView(imgObstacle) : null);

        getChildren().addAll(border,mountain);

        setTranslateX(x * TILE_SIZE);
        setTranslateY(y * TILE_SIZE);

        setOnMouseClicked(e -> setCharacters(getScene().getX(),getLayoutY()));
    }

    public void setCharacters(double x, double y){

        if (home){
            if (mountain.getGraphic() != null ){
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setTitle("Advertencia");
                alert.setContentText("No puedes posicionar la meta sobre un obstaculo");

                alert.show();
            }else{
                homeX = x;
                homeY = y;
                mountain.setGraphic(obstacle ? new ImageView(imgObstacle) : new ImageView(imgHome));
                System.out.println();
                home=false;
            }
        }

        if (player){
            if (mountain.getGraphic() != null ){
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setTitle("Advertencia");
                alert.setContentText("No puedes posicionar al jugador sobre un obstaculo");

                alert.show();
            }else {
                System.out.println(homeX);
                mountain.setGraphic(obstacle ? new ImageView(imgObstacle) : new ImageView(imgPlayer));
                player=false;
            }

        }
    }
}

0 个答案:

没有答案