1

I'm trying to create Tetris. I created GridPane and filled width and height with labels using nested for loop, then putted GridPane in to the scene. How can I change certain label background all labels have coordinates. I want to change label background because then I can make different tetrominoes for the game. Maybe there are better approaches to making tetrominoes in Tetris which I don't know. My code.

public class Menu extends Application {

    private GridPane Grid = new GridPane(); //Layout

    private Label label = new Label();      // Label

    private int height = 600; private int width = 600;  private int pixel = 30;



    @Override
    public void start(Stage Stage) throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

        Stage.setTitle("Tetris");

        Grid.setGridLinesVisible(true);

        Scene scene = new Scene(Grid, width, height); // Add Layout to scene

        FillingLayoutWithLabels(width,height);

        Stage.setScene(scene);

        Stage.show();
    }

    private void FillingLayoutWithLabels(int width, int height) {

        for(int i = 0;i< width/pixel ;i++){

            for(int j = 0;j<height/pixel ; j++){

                addLabel(i,j);
            }
        }
    }

    public void addLabel(int columnIndex, int rowIndex) {

        Label label = new Label();

        label.setMinWidth(pixel);

        label.setMinHeight(pixel);

        label.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));

        label.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, null, new BorderWidths(1))));

        Grid.setColumnIndex(label, columnIndex);

        Grid.setRowIndex(label, rowIndex);

        label.setId(rowIndex+ " " + columnIndex);

        Grid.getChildren().add(label);
    }
}

enter image description here

c0der
  • 18,467
  • 6
  • 33
  • 65
Jules
  • 85
  • 1
  • 12
  • 1
    Does [this](https://stackoverflow.com/questions/20825935/javafx-get-node-by-row-and-column) or [this](https://stackoverflow.com/questions/20655024/javafx-gridpane-retrieve-specific-cell-content) answer your question ? – c0der Feb 11 '20 at 06:19
  • java naming conventions, please! – kleopatra Feb 11 '20 at 08:47
  • Thank you c0der sorry for late response, I tried methods in your suggested links ,but had some problems I found that i need to remove this line `Grid.setGridLinesVisible(true)` to make my code work details why in this [Link](https://stackoverflow.com/questions/29558894/problems-with-accessing-gridpane-node) – Jules Feb 11 '20 at 14:07
  • 1
    `GridPane` and `Label` for this may complicate things. I would use `Shape`. I started a Tetris game a long time ago and never finished it. https://github.com/sedj601/TetrisApp/tree/master/src/tetrisapp – SedJ601 Feb 11 '20 at 15:13
  • 1
    Might be a bad suggestion but I wouldn't use GridPane for something as dynamic as Tetris, I suggest using a Pane or a Canvas and use a unified block size to keep it organised, and move things around using an Animation timer for example. – SDIDSA Feb 11 '20 at 19:08
  • I just went back and looked at the code to the link I posted. I would not recommend using it. – SedJ601 Feb 12 '20 at 21:44

0 Answers0