Firstly, sorry for my English :(
I'm reading "Introduction to Java Programming" and stuck in a exercise: Write a program that displays a tic-tac-toe board. A cell may be X, O, or empty. What to display at each cell is randomly decided.
Below is my code
public class TictactoeBoard extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
GridPane pane = new GridPane();
Image ximage = new Image("x.gif");
Image oimage = new Image("o.gif");
ImageView xImageView = new ImageView(ximage);
ImageView oImageView = new ImageView(oimage);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
int random = (int)(Math.random() * 2);
if(random == 0) {
pane.add(xImageView, j, i);
} else {
pane.add(oImageView, j, i);
}
}
}
Scene scene = new Scene(pane);
primaryStage.setTitle("Tic-tac-toe Board");
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setResizable(false);
}
public static void main(String[] args) {
Application.launch(args);
}
It stuck at pane.add in the for loop. It get this error: http://pastebin.com/TB9kLhrZ
I tried not to use for loop and manually add (for example)
pane.add(oImageView, 0, 2);
pane.add(xImageView, 1, 3);
The application work perfectly. Can someone show me why this problem happen and how to fix it? Thanks you very much!