如何在javafx中刷新窗格

时间:2016-04-20 06:59:42

标签: javafx

我最近一直在学习JavaFX,并且有关于如何刷新窗格的问题。在这个简单的程序中,当我点击移动按钮时,我希望黑色方块向右移动到下一个块,但是它不起作用,我想知道如何修复我的代码。 / p>

screen shot

主类:

public class Main extends Application {
    private Cell cells[] = new Cell[5];
    private Player player;
    private Board board;
    Button move = new Button("move");

    public Main() throws Exception {
        for (int i = 0; i < cells.length; i++) {
            cells[i] = new Cell(i);
        }
        this.player = new Player(0, cells);
        this.board = new Board(player, cells);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Main game = new Main();
        BorderPane pane = new BorderPane();
        pane.setCenter(board);
        pane.setBottom(move);

        Scene scene = new Scene(pane,400,80);
        primaryStage.setTitle("Move");
        primaryStage.setScene(scene); 
        primaryStage.show();

        move.setOnAction(e -> game.move());
    }
    public void move() {
        player.currentCell.index += 1;
        board.paint();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

董事会成员:

class Board extends Pane {
    private Player player;
    public Cell cells[];
    private final int CELLWIDTH = 40;
    private final int CELLHEIGHT = 40;
    private final int LMARGIN = 100;

    public Board(Player p, Cell cells[]) {
        player = p;
        this.cells = cells;
        paint();
    }

    public void paint() {
        Cell cell;
        for (int i=0; i<cells.length; i++) {
            cell = cells[i];
            Rectangle r1 = new Rectangle(xCor(cell.index), 0, CELLWIDTH, CELLHEIGHT); 
            r1.setStroke(Color.BLACK); 
            r1.setFill(Color.WHITE);
            getChildren().add(r1);
        }

        cell = player.currentCell;
        Rectangle r2 = new Rectangle(xCor(cell.index), 0, CELLWIDTH, CELLHEIGHT); 
        r2.setFill(Color.BLACK);
        getChildren().add(r2);
    }
    private int xCor(int col) {
        return LMARGIN + col * CELLWIDTH;
    }
}

玩家等级:

class Player {
    public int position;
    public Cell currentCell;
    public Player(int position, Cell cell[]) throws Exception {
        this.currentCell = cell[0];
    }
}

细胞分类:

class Cell {
    public int index;
    public Cell(int index) {
        this.index = index;
    }
}

1 个答案:

答案 0 :(得分:0)

您可能想要重新编写代码,将播放器的位置存储在Player课程中会让生活变得困难。我还建议在Cell类中添加一个标志,说明玩家是否在里面,例如

class Cell {
    public int index;
    private Player playerInCell;
    public Cell(int index) {
        this.index = index;
    }
    public void setPlayerInCell(Player p){
        this.playerInCell = p;
    }
    public void clearPlayerInCell(){
        this.playerInCell = null;
    }
    public Player getPlayerInCell(){
        return this.playerInCell;
    }
}

然后,在将玩家移动到Cell后,您可以从之前的Cell中清除它们并将其设置为新的Paint()功能,如果玩家在场,为细胞着色。

另一方面,如果您希望坚持使用您的方法,您的问题是由于您只更改了index类的Cell属性,您也应该更改{ {1}}在数组Cell中的位置或仅更改cells[]类的currentCell属性,否则您的播放器始终位于同一位置。以下是更改Player的{​​{1}}属性的示例:

Player

[编辑]

我已经完成了一些主要的代码清理,你做事的一些方法有点奇怪,我希望你不介意,这里有改变的类:

主要

currentCell

<强>板

public void move() {
    Cell currentCell = player.currentCell;
    Cell nextCell = null;
    for (int i = 0; i < cells.length; i++) {
        if (cells[i] == currentCell && i+1 < cells.length){
            nextCell = cells[i+1];
            break;
        }
    }
    if (nextCell != null)
        player.currentCell = nextCell;
    else{
        //Error handling, next cell not found
    }
    board.paint();
}

<强>播放器

public class Main extends Application {

    private Cell cells[] = new Cell[5];
    private Player player;
    private Board board;
    Button move = new Button("move");

    public Main() throws Exception{
        for (int i = 0; i < cells.length; i++) {
            cells[i] = new Cell(i);
        }
        this.player = new Player(cells[0]);
        this.board = new Board(player, cells);
    }

    @Override
    public void start(Stage primaryStage) throws Exception{
        BorderPane pane = new BorderPane();
        pane.setCenter(board);
        pane.setBottom(move);

        Scene scene = new Scene(pane,400,80);
        primaryStage.setTitle("Move");
        primaryStage.setScene(scene);
        primaryStage.show();

        move.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                move();
            }
        });
    }

    public void move() {
        //Get current players cell, we want to move them one right
        Cell currentCell = player.getCurrentCell();
        Cell nextCell = null;
        //Searching for current cell in board, if found we need to clear the player from it and select the next cell
        for (int i = 0; i < cells.length; i++) {
            if (cells[i] == currentCell && i+1 < cells.length){
                cells[i].clearPlayerInCell();
                nextCell = cells[i+1];
                break;
            }
        }
        //We found it, let's move the player
        if (nextCell != null) {
            player.setCurrentCell(nextCell);
            nextCell.setPlayerInCell(player);
        }
        //We didn't find it, or our index was out of range, what do we do now?
        else{
            //Error handling, next cell not found
            //Example, let's put them back at the start
            player.setCurrentCell(cells[0]);
            cells[0].setPlayerInCell(player);
            cells[cells.length-1].clearPlayerInCell();
        }
        board.paint();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

<强>细胞

public class Board extends Pane {
    private Player player;
    private Cell cells[];
    private final int CELLWIDTH = 40;
    private final int CELLHEIGHT = 40;
    private final int LMARGIN = 100;

    public Board(Player p, Cell cells[]) {
        player = p;
        this.cells = cells;
        paint();
    }

    public Cell[] getCells(){
        return this.cells;
    }

    public Player getPlayer() {
        return player;
    }

    public void paint() {
        //Clear previous cells, we don't need them now
        getChildren().clear();
        //Redraw them
        for(Cell cell : cells){
            Rectangle r1 = new Rectangle(xCor(cell.getIndex()), 0, CELLWIDTH, CELLHEIGHT);
            r1.setStroke(Color.BLACK);
            //We've found a player in the cell, let's colour it black
            if (cell.getPlayerInCell() != null)
                r1.setFill(Color.BLACK);
            //No, player in this cell, white it is
            else
                r1.setFill(Color.WHITE);
            getChildren().add(r1);
        }
    }
    private int xCor(int col) {
        return LMARGIN + col * CELLWIDTH;
    }
}

现在可以工作了,我可以移动单元格,我也设置它,以便在玩家到达终点时单元格返回到开头,但这就是一个例子。 它使用public class Player { private Cell currentCell; public Player(Cell cell) throws Exception { this.currentCell = cell; cell.setPlayerInCell(this); } public Cell getCurrentCell(){ return this.currentCell; } public void setCurrentCell(Cell cell){ this.currentCell = cell; } } 的{​​{1}}属性工作,如果它不为null,那么我们知道一个玩家在单元格中并且可以将其着色为黑色。如果它为null,则单元格中没有玩家,我们可以将其着色为白色。这也可以让你在未来拥有更多不同颜色的玩家。虽然我不知道你的最终目标是什么。希望这会有所帮助,如果您想进一步解释,请随时提出

另外,为了进一步阅读,请参阅here为什么最好使用像我这样的getter和setter

此外,这段代码背后的原因是:

public class Cell {
    private int index;
    private Player playerInCell;
    public Cell(int index) {
        this.index = index;
    }
    public void setPlayerInCell(Player p){
        this.playerInCell = p;
    }
    public void clearPlayerInCell(){
        this.playerInCell = null;
    }
    public Player getPlayerInCell(){
        return this.playerInCell;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}

因为我使用的是Java 1.7而不是Java 1.8且无法使用谓词,所以您应该可以安全地将其更改为Cell