I am trying to use two for loops to automatically add ImageView nodes to each location. When using the for loops I receive an error. When I comment the for loop code out with only one statement to add an ImageView node the code seems to work can you use for loops to populate GridPane? If so what am I doing wrong? If not what could be used as a solution?
My Class:
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);
}
}
Edit: for the code:
grid.add(backOfCards, i, j);
I changed the line of code to
grid.add(new ImageView(backOfCardImg), i, j);
this seemed to solve the problem but can anyone explain to me why the first option wouldnt work?
This might be a usable start for a memory game in fx. It uses an own extension of ImageView to do the turn and focus animations and to deal with a common backside image. Its only graphics, no game logic.