在GridPane(JavaFX)中设置单个单元格的颜色

时间:2015-10-28 00:01:23

标签: java javafx colors tetris gridpane

我正在为一个课程项目制作俄罗斯方块,我有想法利用网格窗格将舞台划分为均匀分布的行和列。我打算单独更改每个单元格的颜色以模拟实际的碎片,但我不知道从哪里开始。我尝试添加彩色矩形,但这不起作用。

颜色取自名为colorArray的2D数组,即15X10,每个索引都有一个特定的颜色。

public void start(Stage primaryStage) throws Exception {
    pane = new GridPane();
    pane.setGridLinesVisible(true);
    pane.setVgap(40);
    pane.setHgap(40);
    primaryStage.setResizable(false);
    scene = new Scene(pane,height,width);
    for(int x = 0; x < 16; x++){
        pane.getRowConstraints().addAll(getFifteenRowConstraints());
        for(int y = 0; y<10; y++){
            pane.getColumnConstraints().addAll(getTenColumnConstraints());


        }
    }
    primaryStage.setScene(scene);
    primaryStage.setTitle("Russian Game");
    primaryStage.show();
}

我想我不确定它是什么行和col约束,我所知道的是我已经均匀地放置了由线分隔的单元格。

我如何分别改变每个细胞的颜色?

1 个答案:

答案 0 :(得分:0)

查看JavaFX中的Rectangle类。以下是在JavaFX中绘制为黑色的矩形的示例。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Driver extends Application {

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

  @Override
  public void start(Stage primaryStage) {
    Group group = new Group();

    Rectangle rect = new Rectangle(20,20,200,200);

    rect.setStroke(Color.BLACK);
    group.getChildren().add(rect);

    Scene scene = new Scene(group, 300, 200);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}