JavaFX Cell Spacing?

时间:2015-01-13 21:57:50

标签: java user-interface javafx

这是我正在制作的生活游戏的代码,我想尝试在活细胞之间添加间距,但无法弄明白。

有人可以帮忙吗?或者是否可以在后台制作网格?

public class ClickFX extends Application 
{
    public LifeGrid lg;
    public int x;
    public int y;

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

    GraphicsContext gc;
    ClickData clickData;
    int squareSize;
    Stage primaryStage;
    Color colours[] = { Color.WHITE, Color.RED, Color.GREEN, Color.CYAN,
            Color.BLUE, Color.BLACK };

    public void start(Stage primaryStage) throws FileNotFoundException {
        squareSize = 6;
        int x = 80, y = 80;
        clickData = new ClickData(x, y);
        lg = new LifeGrid(x,y,"seed.txt");

        VBox root = new VBox(8);
        HBox buttons = new HBox(5);

        buttons.setAlignment(Pos.CENTER);

        Button ngButton = new Button("Next Gen");
        ngButton.setOnAction(new ngButtonHandler());
        ngButton.setTooltip(new Tooltip("Click to Start!\nKeep clicking to Evolve!"));

        Button clearButton = new Button("Clear");
        clearButton.setOnAction(new clearButtonHandler());
        clearButton.setTooltip(new Tooltip("Click to Clear"));

        Button randomButton = new Button("Random Generate");
        randomButton.setOnAction(new randomButtonHandler());
        randomButton.setTooltip(new Tooltip("Click to create Random Generation"));

        Button closeButton = new Button("Close");
        closeButton.setOnAction(new closeButtonHandler());

        buttons.getChildren().addAll(ngButton, randomButton, clearButton, closeButton);

        Canvas canvas = new Canvas(x * squareSize, y * squareSize);
        gc = canvas.getGraphicsContext2D();

        canvas.addEventHandler(MouseEvent.MOUSE_CLICKED, new mouseClickHandler());
        this.primaryStage = primaryStage;

        primaryStage.setTitle("Game Of Life");
        root.getChildren().addAll(canvas, buttons);
        primaryStage.setScene(new Scene(root));![enter image description here][1]
        primaryStage.setResizable(false);
        primaryStage.show();

        }        


    public void init() throws Exception
    {
        super.init();
        Parameters parameters = getParameters();

        Map<String, String> namedParameters = parameters.getNamed();

        for(Map.Entry<String, String> entry : namedParameters.entrySet())
        {
            if("width".equals(entry.getKey()))
            {
                x = Integer.parseInt(entry.getValue());
            }
            else if("height".equals(entry.getKey()))
            {
                y = Integer.parseInt(entry.getValue());
            }
        }
    }

按钮的操作在下面创建。

class clearButtonHandler implements EventHandler<ActionEvent> {
            public void handle(ActionEvent e) {
                clickData.clear();
                gc.setFill(Color.WHITE);
                gc.fillRect(0, 0, clickData.getX() * squareSize, clickData.getY()
                        * squareSize);
            }
        }

        class closeButtonHandler implements EventHandler<ActionEvent> {
            public void handle(ActionEvent e) {
                primaryStage.close();
            }
        }

        class randomButtonHandler implements EventHandler<ActionEvent>
        {
            public void handle(ActionEvent e)
            {
                lg.RandomGeneration();
                drawNG();
            }
        }

        class ngButtonHandler implements EventHandler<ActionEvent>
        {
            public void handle(ActionEvent e)
            {
                drawNG();
                lg.Run();
            }
        }

在这里,我在画布上绘制了活细胞。

    public void drawNG()
    {
        for(int i=0; i<lg.CG.length; i++)
        {
            for(int k=0; k<lg.CG[0].length; k++)
            {
                if(lg.CG[i][k] == 1)
                {
                    gc.setFill(Color.BLACK);
                    gc.setStroke(Color.RED);
                    gc.fillRect(k*squareSize, i*squareSize, squareSize, squareSize);               
                }
                else
                {
                    gc.setFill(Color.WHITE);
                    gc.fillRect(k*squareSize, i*squareSize, squareSize, squareSize);
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

更改

gc.fillRect(k*squareSize, i*squareSize, squareSize, squareSize)

为...

gc.fillRect(k*squareSize + offset, i*squareSize + offset, squareSize, squareSize)

其中offset是您想要的距离。

相关问题