2016-04-20 16 views
0

最近、JavaFXを学習していて、ペインをリフレッシュする方法に関する問題があります。このシンプルなプログラムでは、移動ボタンをクリックしているときに黒い四角を右のブロックに移動したいが、動きません。コードを修正するにはどうすればいいですか?javafxでペインをリフレッシュする方法

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; 
    } 
} 
+0

私が投稿したコードをチェックしましたか?あなたの問題を解決しましたか? – Draken

+1

なぜ 'Main'の2番目のインスタンスを作成しますか? Shcouldは 'move.setOnAction(e - > this.move());'さらに 'Rectangle'sを' Board'に追加しますが、絶対に削除しないでください。それは目的ですか? – fabian

+0

答えを更新し、それが私の上で働いて、助けてくれることを望みました – Draken

答えて

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()機能でそれらを設定することができますときに、内のセルに色を。

あなたのメソッドに固執したい場合は、Cellクラスのindexプロパティを変更するだけで問題が発生している場合は、配列cells[]Cellの位置を変更するか、のcurrentCellプロパティ0クラスでなければ、プレイヤーは常に同じ場所にとどまります。ここでPlayercurrentCellプロパティを変更する例は次のとおりです。

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

私はあなたが物事をやっていたいくつかの方法が少し奇妙だった、いくつかの主要なコードのクリーンアップをやった

[編集]、Iあなたが気にしない願って、ここで変更クラスは次のとおりです。

メイン

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

セル

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

は、私はそれをも設定しましたプレイヤーが最後に達した場合、セルは最初に戻りますが、それは実例ですル。 CellプロパティがplayerInCellで動作します。ヌルでない場合は、プレーヤーがセル内にあり、黒色にすることができます。値がnullの場合、セル内にプレーヤーは存在せず、色を白にすることができます。これにより、将来的には異なる色のプレーヤーが増える可能性もあります。あなたの最終目標が何であるか分かりませんが。この情報がお役に立てば幸いですし、あなたはそれ以上の説明をしたい場合、私は

も持っているように、それはゲッターとセッターを使用することをお勧めしますなぜ、このビットの背後にある理由をhereを参照して、さらに読書のために、また

気軽にコード:

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

私は、Java 1.7を使用しての代わりに、Javaの1.8と述語を使用することはできません、あなたが代わりにmove.setOnAction(e -> this.move());にそれを変更しても安全であるべきだからです。

+0

ボタンをクリックした後も、プレーヤーのcurrentCellは変更されましたが、黒い四角は移動しません。 – Chen

+0

ああ、私は別の問題、一瞬を見ました – Draken

関連する問題