(Game: display a checkerboard) Write a program that displays a checkerboard
in which each white and black cell is a Rectangle with a fill color black or
white, as shown in figure.
Solution:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Checkboard extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
int i = 0;
int j = 0;
for(i = 0; i < 8; i++) {
for(j = 0; j < 8; j++) {
Rectangle rec = new Rectangle(20 * j, 20 * i, 20, 20);
if(i + j % 2 == 1) {
rec.setFill(Color.BLACK);
} else {
rec.setFill(Color.WHITE);
}
pane.getChildren().add(rec);
}
}
Scene scene = new Scene(pane, 160, 160);
primaryStage.setTitle("Checkboard");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
5
in which each white and black cell is a Rectangle with a fill color black or
white, as shown in figure.
Solution:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Checkboard extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
int i = 0;
int j = 0;
for(i = 0; i < 8; i++) {
for(j = 0; j < 8; j++) {
Rectangle rec = new Rectangle(20 * j, 20 * i, 20, 20);
if(i + j % 2 == 1) {
rec.setFill(Color.BLACK);
} else {
rec.setFill(Color.WHITE);
}
pane.getChildren().add(rec);
}
}
Scene scene = new Scene(pane, 160, 160);
primaryStage.setTitle("Checkboard");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}