title: JavaFX code duplication
body: <p>I have 22 piece objects (11 white pieces and 11 black pieces). They all have a color and a letter. I need to add all those objects with an image to a HBox in javafx. </p>
  
  <p>I have the following code that works:</p>
  
  <pre><code>public void draw(){
      Paint border;
      Paint fill;
      for (Piece piecesObjects : pieces){
          Group group = new Group();
          Hexagon hexagon = new Hexagon();
          if (piecesObjects.isWhite()){
              border = Color.WHITE;
              fill = Color.BLACK;
              ImageView imageView = new ImageView("/hive/imagesPieces/b/" + piecesObjects.getPiece() + ".png");
              group.getChildren().addAll(hexagon,imageView);
              whitePieces.getChildren().add(group);
          } else {
              border = Color.BLACK;
              fill = Color.WHITE;
              ImageView imageView = new ImageView("/hive/imagesPieces/w/" + piecesObjects.getPiece() + ".png");
              group.getChildren().addAll(hexagon,imageView);
              blackPieces.getChildren().add(group);
          }
          hexagon.setStroke(border);
          hexagon.setFill(fill);
      }
  }
  </code></pre>
  
  <p>As you can see there is a lot of duplication and I was wondering how to fix this. I tried to do the following: </p>
  
  <pre><code> public void draw(HBox hbox){
      Paint border;
      Paint fill;
      for (Piece piecesObjects : pieces){
          Group group = new Group();
          Hexagon hexagon = new Hexagon();
          if (piecesObjects.isWhite()){
              border = Color.WHITE;
              fill = Color.BLACK;
          } else {
              border = Color.BLACK;
              fill = Color.WHITE;
          }
          hexagon.setStroke(border);
          hexagon.setFill(fill);
          ImageView imageView = new ImageView("/hive/imagesPieces/" + pieceObject.getColor() + "/" + piecesObjects.getPiece() + ".png");
          group.getChildren().addAll(hexagon,imageView);
          hBox.getChildren().add(group);
      }
  }
  
  public void drawWhitepieces(){
      draw(whitePieces);
  }
  
  public void drawBlackpieces(){
      draw(blackPieces);
  }
  </code></pre>
  
  <p>But this code still draws 22 pieces in each HBox what shouldn't be allowed. (normal because it draws 22 hexagons).</p>