为什么网格窗格中的节点位置会影响网格的布局?

时间:2018-10-20 21:23:45

标签: java javafx gridpane

我正在尝试使用JavaFX中的GridPane重新创建“蛇”游戏。除此特定错误外,我的代码似乎正常运行,在此情况下,每当我使用键将绿色节点(标题为head)朝向黄色节点(标题为food)遍历时,网格的行或列似乎缩短了一个单位,导致网格以某种方式崩溃。有没有办法阻止GridPane调整大小?下面是我的代码:

package snake;

import java.util.ArrayList;
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class snakemain extends Application {
    Random random = new Random();
    int posX = random.nextInt(21), posY = random.nextInt(21), foodposX = random.nextInt(24), foodposY = random.nextInt(24);

    public static void main(String... args) {
        launch(args);
    }

    public void start(Stage stage) {
        //Creates Scene and stage setting + gridpane
        stage.setTitle("Snake by Yeldor");
        GridPane gridpane = new GridPane();
        Random random = new Random();
        Scene scene = new Scene(gridpane, 500, 500);
        scene.setFill(Color.WHITE);
        gridpane.setGridLinesVisible(true);
        gridpane.setHgap(20);
        gridpane.setVgap(20);
        //creates head of snake, Arraylist of snakes body parts, and food consumable for snake
        ArrayList<Rectangle> snakeBody = new ArrayList<Rectangle>();
        Rectangle head = new Rectangle(20,20,Color.GREEN.brighter().brighter());
        //invisible block to manage grid
        Rectangle food = new Rectangle(20,20, Color.YELLOW.brighter().brighter());
        //adds rectangles to grippane
        gridpane.add(head, posX, posY);
        gridpane.add(food, foodposX, foodposY);
        //makes food non mangaged by the gridpane
        scene.setOnKeyPressed(e -> 
        {
            String s = e.getCode().toString();
        try {

            switch(s) {
            case "W":   gridpane.getChildren().remove(head);
                        gridpane.add(head, posX, --posY);
                    break;
            case "A":   gridpane.getChildren().remove(head);
                        gridpane.add(head, --posX, posY);
                    break;
            case "S":   gridpane.getChildren().remove(head);
                        gridpane.add(head, posX, ++posY);
                    break;
            case "D":   gridpane.getChildren().remove(head);
                        gridpane.add(head, ++posX, posY);
                    break;
            }


            if (posX == 24 || posY == 24 || posX == -1 || posY == -1) {
                missionFailed();
                stage.close();
            }

        }

        catch(IllegalArgumentException error) {
            missionFailed();
            stage.close();
        }

        });

        stage.setScene(scene);
        stage.show();

    }

    void missionFailed() {
        Stage failedPopup = new Stage();
        failedPopup.setTitle("You Died!");
        failedPopup.initModality(Modality.APPLICATION_MODAL);
        Button OK = new Button("OK");
        Group group = new Group();
        Scene miniScene = new Scene(group, 150, 100);
        group.getChildren().add(OK);
        failedPopup.setScene(miniScene);
        failedPopup.show();
        OK.setOnMouseClicked(q -> {
            failedPopup.close();
        });
    }

}

2 个答案:

答案 0 :(得分:0)

使用hgapvgap不会修改列的大小,它会修改列之间的 空格 。如果您例如将Rectangle彼此水平放置(相同的行索引,列索引相差1),两个节点之间的距离为vgap;恰好有2列被填充,并且宽度!=0。现在将Rectangle移到相同的单元格,只有一个宽度!= 0的单列会导致意外结果。

相反,您应该限制行/列的大小:

// gridpane.setHgap(20);
// gridpane.setVgap(20);

int rows = 500 / 20;
int columns = 500 / 20;

RowConstraints rConstraints = new RowConstraints(20);
ColumnConstraints cConstraints = new ColumnConstraints(20);

for (int i = 0; i < columns; i++) {
    gridpane.getColumnConstraints().add(cConstraints);
}
for (int i = 0; i < rows; i++) {
    gridpane.getRowConstraints().add(rConstraints);
}

答案 1 :(得分:0)

另一个对我有用的解决方案是用透明的矩形填充整个区域,然后设置当前使用的矩形的颜色。