1

我正在尝试使用两个 for 循环自动将 ImageView 节点添加到每个位置。使用 for 循环时,我收到一个错误。当我仅使用一条语句将 for 循环代码注释掉以添加 ImageView 节点时,代码似乎可以工作,您可以使用 for 循环来填充 GridPane 吗?如果是这样,我做错了什么?如果不是什么可以用作解决方案?

我的课:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class GridCreation extends Application {
@Override
public void start(Stage gameStage) throws Exception {
    GridPane grid = new GridPane();
    Image backOfCardsImg = new Image("images/naruto_shipuden_logo.png");
    ImageView backOfCards = new ImageView(backOfCardsImg);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                grid.add(backOfCards, i, j);

            }
        }

    //grid.add(backOfCards, 1,1);
    Scene scene = new Scene(grid);
    gameStage.setTitle("MemoryGame");
    gameStage.setScene(scene);
    gameStage.show();
}

public static void main(String[] args) {
    launch(args);
}
}

编辑:对于代码:

grid.add(backOfCards, i, j);

我将代码行更改为

grid.add(new ImageView(backOfCardImg), i, j);

这似乎解决了问题,但谁能向我解释为什么第一个选项不起作用?

4

1 回答 1

1

对于 fx 中的记忆游戏,这可能是一个有用的开始。它使用自己的 ImageView 扩展来执行转弯和聚焦动画并处理常见的背面图像。它只有图形,没有游戏逻辑。

public class MemoryGame extends Application {
    final int rows = 4;
    final int columns = 4;
    CardView views[][] = new CardView[rows][];

    public static class CardView extends ImageView {
        static final double scale = 0.95;
        static DropShadow shadowhoover = new DropShadow(5, 4, 4, Color.rgb(50, 60, 50));
        static DropShadow shadowdown = new DropShadow(2, 2, 2, Color.rgb(50, 60, 50));
        static Image backside = null;
        public static void setbackside(Image image) { backside = image; }

        public CardView(Image image) { 
            super(backside); 
            setRotationAxis(new Point3D(0, 200,0));
            setScaleX(scale);
            setScaleY(scale);
            setEffect(shadowdown);
            setOnMouseEntered(m -> {
                setEffect(shadowhoover);
                setScaleX(scale*1.01);
                setScaleY(scale*1.01);
            });
            setOnMouseExited(m -> {
                setEffect(shadowdown);
                setScaleX(scale);
                setScaleY(scale);
            });
            setOnMouseClicked(m -> {      
                RotateTransition r1 = new RotateTransition(Duration.millis(300), this);
                r1.setByAngle(90);
                r1.setOnFinished(e -> setImage(image));
                RotateTransition r2 = new RotateTransition(Duration.millis(300), this);
                r2.setByAngle(-90);

                RotateTransition r3 = new RotateTransition(Duration.millis(300), this);
                r3.setByAngle(90);
                r3.setOnFinished(e -> setImage(backside));
                RotateTransition r4 = new RotateTransition(Duration.millis(300), this);
                r4.setByAngle(-90);

                new SequentialTransition(r1, r2, new PauseTransition(Duration.millis(1000)), r3, r4).play();
            });
        }
    }

    @Override
    public void start(Stage gameStage) throws Exception {
        GridPane grid = new GridPane();
        grid.setBackground(new Background(new BackgroundFill(Color.rgb(140, 200, 140), new CornerRadii(0), new Insets(0))));
        grid.setHgap(5);
        grid.setVgap(5);
        Image back = new Image(MemoryGame.class.getResource("card-back.png").toExternalForm(), 140, 200, true, true);
        Image front = new Image(MemoryGame.class.getResource("card-1.png").toExternalForm(), 140, 200, true, true);
        CardView.setbackside(back);
        for (int r = 0; r < rows; r++) {
            views[r] = new CardView[columns];
            for (int c = 0; c < columns; c++) {
                CardView view = new CardView(front); // different front images of course...
                views[r][c] = view;

                HBox box = new HBox(5);
                box.getChildren().add(views[r][c]);
                grid.add(box, c, r);

            }
        }

        //grid.add(backOfCards, 1,1);
        Scene scene = new Scene(grid);
        gameStage.setTitle("MemoryGame");
        gameStage.setScene(scene);
        gameStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
于 2014-12-07T12:37:40.980 回答