JavaFX 代码重复

标签 java javafx duplicates code-duplication

我有 22 block 物体(11 block 白色 block 和 11 block 黑色 block )。它们都有颜色和字母。我需要将所有带有图像的对象添加到 javafx 中的 HBox 中。

我有以下有效代码:

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

正如你所看到的,有很多重复,我想知道如何解决这个问题。我尝试执行以下操作:

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

但是这段代码仍然在每个 HBox 中绘制了 22 block ,这是不应该允许的。 (正常,因为它绘制了 22 个六边形)。

最佳答案

为单个片段而不是多个片段创建辅助方法。现在,您的方法版本会遍历片段列表并添加它们,而不管颜色如何,这显然不是您想要的。

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

关于JavaFX 代码重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36331322/

相关文章:

java - Java删除字符串中相邻的重复项

java - 如果一个单词有 5 个字母,则每行打印一个字母,如果没有,则只打印该单词?

javascript - Node.js、socket.io : Receiving duplicate requests from every socket. io 调用

JAVA OpenGL 3D Cube 多个纹理

java - Scenebuilder安装后显示错误

java - 在 JavaFX 中设置按钮操作时出现 "Cannot find symbol constructor, EventHandler does not take parameters"

Javafx HBox 限制到特定高度

sql - sql从选择中插入到表中,没有重复(需要更多然后是DISTINCT)

java - 带有 Rest 的 Apache AVRO

java - 为什么我的 JNA 使用应用程序没有以正确的方式使用react?