Search code examples
javajavafxduplicatescode-duplication

JavaFX code duplication


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.

I have the following code that works:

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

As you can see there is a lot of duplication and I was wondering how to fix this. I tried to do the following:

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

But this code still draws 22 pieces in each HBox what shouldn't be allowed. (normal because it draws 22 hexagons).


Solution

  • Create a helper method for a single piece, not for multiple pieces. Right now your version of the method goes through the list of pieces and adds them regardless of the color, which is obviously not what you want.

    private draw(Pane parent, Piece piece, Color stroke, String dir) {
        Hexagon hexagon = new Hexagon();
        hexagon.setStroke(stroke);
        hexagon.setFill(stroke.invert());
    
        ImageView imageView = new ImageView("/hive/imagesPieces/" + dir + "/" + piece.getPiece() + ".png");
        Group group = new Group(hexagon, imageView);
        parent.getChildren().add(group);
    }
    
    public void draw() {
        for (Piece piece : pieces) {
            if (piece.isWhite()){
                draw(whitePieces, piece, Color.WHITE, "b");
            } else {
                draw(blackPieces, piece, Color.BLACK, "w");
            }
        }
    }