I am trying to build the following rectangular grid of stars in which the second and third rows are offset to the right to achieve the design pattern.
However, with my code all I have achieved is this:
My code is:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import java.util.Random;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
GridPane grid = new GridPane();
Random rand = new Random();
int colNum = 0;
//Shapes
for (int row = 0; row < 4; row++) {
if (row == 1 || row == 3)
colNum = 6;
else
colNum = 7;
for (int col = 0; col < colNum; col++) {
Polygon star = new Polygon(25, 0, 15, 20, 0, 20, 10, 40, 5, 60, 25, 50, 45, 60, 40, 40, 50, 20, 35, 20);
star.setFill(Color.WHITE);
star.setStroke(Color.BLACK);
GridPane.setRowIndex(star, row);
GridPane.setColumnIndex(star, col);
GridPane.setHalignment(star, HPos.RIGHT);
grid.getChildren().addAll(star);
}
}
Scene scene = new Scene(grid, 1024, 800, true);
primaryStage.setScene(scene);
primaryStage.show();
}
}
How can I offset the second and fourth row so that it looks like the first image?


