2017-04-22 13 views
0

私は、以下に示すようにドラッグ可能な部分でチェスボードを実装しようとしています。しかし、私は、他のノードの前にそのノードの下または右にとどまるようにドラッグされている部分を保持し続けることができません。左と上はうまくいくようです。他のノードの前にノードをドラッグし続ける方法(JavaFX 8)?

私はJavaはNode SがそのParentに追加された順序に基づいて、Zインデックスを割り当てる読み取るように私は、それだけで後の最初の市松模様の背景とすべての部分を形成するStackPane Sを宣言することによってこの問題を解決しようとしましたs。このアプローチは以下に反映されています。私もGroupを作成して、を使用できるように、StackPanesとImageViewsを両方とも追加しようとしました。座標ラベルのみが表示されました。

私は後に機能することができますか?

この方法では、ボードを作成します。

public Parent chessBoard() { 
    GridPane board = new GridPane(); 
    StackPane[][] cells = new StackPane[8][8]; 

    // Create the board first 
    // (For dragging pieces to work correctly, draggable pieces must be 
    // added after the whole board, since z-index cannot be set explicitly 
    // in JavaFX. 
    for (int row = 0; row < 10; row++) { 
     for (int col = 0; col < 10; col++) { 
      // x and y in chess coordinate system (0-indexed) 
      int[] invertedY = {-1,7,6,5,4,3,2,1,0,-1}; 
      int x = col - 1; 
      int y = invertedY[row]; 

      // Coordinate labels 
      String[] abcLabels = {"A","B","C","D","E","F","G","H"}; 

      if (row == 9 || row == 0) { 
       if (col == 0 || col == 9) continue; 

       Label label = new Label(abcLabels[x]); 
       label.setTextAlignment(TextAlignment.CENTER); 
       board.add(label, col, row); 

       continue; 
      } else if (col == 0 || col == 9) { 
       Label label = new Label(Integer.toString(y + 1)); 
       board.add(label, col, row); 

       continue; 
      } 

      // Cell background color 
      Square square = game.getBoard().getSquare(x, y); 
      Color color = square.getColor() == ChessColor.BLACK 
        ? Color.PERU : Color.BLANCHEDALMOND; 

      StackPane cell = cells[y][x] = new StackPane(); 
      cell.setMaxSize(60, 60); 
      cell.setMinSize(60, 60); 
      cell.setBackground(new Background(
        new BackgroundFill(color, null, null))); 

      board.add(cell, col, row); 
     } 
    } 

    // Finally, add pieces to their respective cells 
    for (int y = 0; y < 8; y++) { 
     for (int x = 0; x < 8; x++) { 
      Square square = game.getBoard().getSquare(x, y); 
      Piece occupant = square.getOccupant(); 

      if (occupant != null) { 
       String path = "/resources/" + occupant + ".png"; 
       Image image = 
         new Image(getClass().getResourceAsStream(path)); 
       DraggablePieceIcon imageView = 
         new DraggablePieceIcon(image); 
       imageView.setManaged(false); 
       cells[y][x].getChildren().add(imageView); 
      } 
     } 
    } 



    return board; 
} 

このクラスは、ドラッグ可能なアイコンを作る:

public class DraggablePieceIcon extends ImageView { 
    private double mouseX; 
    private double mouseY; 

    public DraggablePieceIcon(Image image) { 
     super(image); 

     setOnMousePressed(event -> { 
      mouseX = event.getSceneX(); 
      mouseY = event.getSceneY(); 
     }); 

     setOnMouseDragged(event -> { 
      double deltaX = event.getSceneX() - mouseX; 
      double deltaY = event.getSceneY() - mouseY; 

      relocate(getLayoutX() + deltaX, getLayoutY() + deltaY); 

      mouseX = event.getSceneX(); 
      mouseY = event.getSceneY(); 
     }); 
    } 
} 

そして、ここでは、私が見ているものです:

enter image description here

答えて

1

あなたは」セルを行ごとに左から右に追加し直します。あなたがセルにピースを追加するので、セルの子孫は、セルの内容を、セルの上または同じ行にあり、セルに残し、他のすべてのセルの内容でカバーされます。この問題を解決するには

あなたはGridPane内の最上位ノードドラッグしたアイテムの親を作ることができる:このソリューションは、細胞彼らの作品の子供を作るいくつかのロジックを実装するためにあなたを必要とすること

public DraggablePieceIcon(Image image) { 
    super(image); 

    setOnMousePressed(event -> { 
     mouseX = event.getSceneX(); 
     mouseY = event.getSceneY(); 

     // make cell containing this piece the top-most cell 
     this.getParent().toFront() 
    }); 

    ... 
} 

注意それらのセルの中心にピースを移動して移動させます。それ以外の場合は、後でそのようなセルの内部にピースをドラッグすると、ピースを他のセルで覆うことができます。

代わりにGridPaneの子を作ることもできます。とにかく、細胞とは無関係に断片をドラッグすることができます。セルとピースとの間の関係は、ビューのためではなく、モデルのために重要であり(この場合、チェス規則の実施)、通常、これらの部分は別々に保持される。

関連する問題