【问题标题】:JavaFX Cell Spacing?JavaFX 单元格间距?
【发布时间】:2015-03-12 00:07:58
【问题描述】:

这是我正在创建的生活游戏的代码,我想尝试在活动单元格之间添加间距,但无法弄清楚。

有人可以帮忙吗?还是可以在后台做一个网格?

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);
                }
            }
        }
    }

【问题讨论】:

  • 更改您的 fillRect 方法调用以向 x 和 y 坐标添加偏移量。
  • @colti - 你能举个例子吗?我会更好地理解
  • 现在每个单元格之间的宽度、高度、水平距离和垂直距离都等于squareSize。要填充单元格,您需要它们之间的距离大于大小。

标签: java user-interface javafx


【解决方案1】:

改变

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

到...

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

offset 是您希望它们之间的距离。

【讨论】:

    猜你喜欢
    • 2019-01-31
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 2013-06-18
    相关资源
    最近更新 更多