javafx如何使用随机颜色填充网格
我有一个网格。 现在网格的背景为浅灰色。 但是我怎样才能得到随机颜色的网格背景呢?所以不是一种颜色 我怎么做这个 有人能帮我吗 提前谢谢 代码是:javafx如何使用随机颜色填充网格,java,javafx,Java,Javafx,我有一个网格。 现在网格的背景为浅灰色。 但是我怎样才能得到随机颜色的网格背景呢?所以不是一种颜色 我怎么做这个 有人能帮我吗 提前谢谢 代码是: public class Grid2 extends Application { double gridSize = 20; @Override public void start(Stage primaryStage) { Scene scene = new Scene(new Group(), 800, 6
public class Grid2 extends Application {
double gridSize = 20;
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(new Group(), 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
scene.setFill(createGridPattern());
}
public ImagePattern createGridPattern() {
double w = gridSize;
double h = gridSize;
Canvas canvas = new Canvas(w, h);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setStroke(Color.BLACK);
gc.setFill(Color.LIGHTGRAY.deriveColor(1, 1, 1, 0.2));
gc.fillRect(0, 0, w, h);
gc.strokeRect(0, 0, w, h);
Image image = canvas.snapshot(new SnapshotParameters(), null);
ImagePattern pattern = new ImagePattern(image, 0, 0, w, h, false);
return pattern;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
您可能可以为网格的每个单元格添加一个新的Jpanel()对象,以获得不同的颜色。通过使某些特定单元组中的不同组件,甚至一个单元组中的不同组件,可以使同一个单元具有不同的背景色 你能举个例子吗?