14.2(Tic-tac-toe board) Write a program that displays a tic-tac-toe board, as shown in Figure 14.43b. A cell may be X, O, or empty. What to display at each cell is randomly decided. The X and O are images in the files x.gif and o.gif
package section_14;
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 _02 extends Application {
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int n = (int) (Math.random() * 3);
if (n == 0)
pane.add(new ImageView(new Image("image/x.gif")), j, i);
else if (n == 1)
pane.add(new ImageView(new Image("image/o.gif")), j, i);
else
continue;
}
}
Scene scene = new Scene(pane, 120, 130);
primaryStage.setTitle("Exercise_14_02");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}