JavaFX画布不显示图形
首先我想说的是,在过去的一天里,我已经对这个问题进行了广泛的研究,但我没有发现任何适合这个特殊情况的东西。我正在尝试创建一个简单的Conway's Game of Life应用程序,而我似乎在绘制JavaFX画布时遇到了麻烦。一切似乎都配置正确(JavaFX画布不显示图形,java,animation,javafx,Java,Animation,Javafx,首先我想说的是,在过去的一天里,我已经对这个问题进行了广泛的研究,但我没有发现任何适合这个特殊情况的东西。我正在尝试创建一个简单的Conway's Game of Life应用程序,而我似乎在绘制JavaFX画布时遇到了麻烦。一切似乎都配置正确(Canvas添加到布局容器,所述容器添加到场景,舞台启动),但即使尝试了不同的封装代码的方法,Canvas仍然是空的 这是我的密码: public class Life extends Application implements Runnable {
Canvas
添加到布局容器,所述容器添加到场景,舞台启动),但即使尝试了不同的封装代码的方法,Canvas
仍然是空的
这是我的密码:
public class Life extends Application implements Runnable {
private Grid grid;
private boolean running;
private boolean paused;
private int stepsPerMinute;
@Override
public void start(Stage stage) {
this.grid = new Grid(60, 40);
this.running = true;
this.paused = false;
this.stepsPerMinute = 60;
StackPane root = new StackPane();
root.getChildren().add(grid);
Scene scene = new Scene(root, 1080, 720);
stage.setTitle("Conway's Game of Life");
stage.setScene(scene);
stage.setResizable(false);
stage.setOnCloseRequest(e -> stop());
stage.show();
}
public void run() {
while (running) {
if (!paused) {
try {
Thread.sleep(1000 / stepsPerMinute);
} catch (InterruptedException e) { e.printStackTrace(); }
grid.step();
grid.draw();
}
}
}
@Override
public void stop() {
this.running = false;
// TODO: Dump Stats at end
System.exit(0);
}
public static void main(String[] args) { launch(args); }
public class Grid extends Canvas {
private final int WIDTH = 1080, HEIGHT = 720;
private boolean[][] cellStates;
private int rows, cols;
private int stepNum;
public Grid(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.stepNum = 0;
randomize();
minWidth(WIDTH);
maxWidth(WIDTH);
prefWidth(WIDTH);
minHeight(HEIGHT);
maxHeight(HEIGHT);
prefHeight(HEIGHT);
}
public Grid(boolean[][] cellStates) {
this.cellStates = cellStates;
this.rows = cellStates.length;
this.cols = cellStates[0].length;
this.stepNum = 0;
}
public void step() {
boolean[][] updatedStates = new boolean[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
int aliveNeighbors = countAliveNeighbors(i, j);
if (cellStates[i][j]) {
if (aliveNeighbors == 2 || aliveNeighbors == 3)
updatedStates[i][j] = true;
}
else if (aliveNeighbors == 3)
updatedStates[i][j] = true;
}
this.cellStates = updatedStates;
stepNum++;
}
private int countAliveNeighbors(int r, int c) {
int result = 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++) {
if (cellStates[ (rows + r + i) % rows ][ (cols + c + j) % cols ])
result++;
}
return result;
}
public void randomize() {
boolean[][] updatedStates = new boolean[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
if (Math.random() < 0.08)
updatedStates[i][j] = true;
}
this.cellStates = updatedStates;
}
public void glider(){
this.cellStates = new boolean[rows][cols];
this.cellStates[1][2] = true;
this.cellStates[2][3] = true;
this.cellStates[3][1] = true;
this.cellStates[3][2] = true;
this.cellStates[3][3] = true;
}
public void draw() {
GraphicsContext g = this.getGraphicsContext2D();
int cellWidth = WIDTH / cols, cellHeight = HEIGHT / rows;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
if (cellStates[i][j])
g.setFill(Color.color(0.0078, 0, 1));
else
g.setFill(Color.color(0.9961, 1, 0.8431));
g.fillRect(j * cellWidth, i * cellHeight, cellWidth, cellHeight);
g.strokeLine(j * cellWidth, 0, j * cellWidth, HEIGHT);
g.strokeLine(0, i * cellHeight, WIDTH, i * cellHeight);
}
}
}
}
公共类生命扩展应用程序实现可运行{
私有电网;
私有布尔运行;
私有布尔运算暂停;
私密的精巢;
@凌驾
公众假期开始(阶段){
this.grid=新网格(60,40);
这是真的;
this.pause=false;
这一步精子数=60;
StackPane root=新的StackPane();
root.getChildren().add(网格);
场景=新场景(根,1080720);
舞台。片名(“康威的人生游戏”);
舞台场景;
阶段。可设置大小(假);
stage.setOnCloseRequest(e->stop());
stage.show();
}
公开募捐{
(跑步时){
如果(!暂停){
试一试{
睡眠(1000/次/分钟);
}catch(InterruptedException e){e.printStackTrace();}
grid.step();
grid.draw();
}
}
}
@凌驾
公共停车场(){
this.running=false;
//TODO:在末尾转储统计信息
系统出口(0);
}
公共静态void main(字符串[]args){launch(args);}
公共类网格扩展画布{
专用最终整型宽度=1080,高度=720;
私有布尔[][]单元状态;
私有int行,cols;
私有int-stepNum;
公共网格(整数行,整数列){
this.rows=行;
this.cols=cols;
this.stepNum=0;
随机化();
最小宽度(宽度);
最大宽度(宽度);
宽度(宽度);
最小高度(高度);
最大高度(高度);
高度(高度);
}
公共网格(布尔[][]单元状态){
this.cellStates=cellStates;
this.rows=cellStates.length;
this.cols=cellStates[0]。长度;
this.stepNum=0;
}
公共无效步骤(){
布尔[][]更新状态=新布尔[行][cols];
对于(int i=0;i 对于(int i=-1;i为什么应用程序
类实现可运行
?您希望它的运行()
方法被调用?你从哪里开始该线程?你应该看看谢谢!我今天晚些时候有机会的时候会尝试实现它。你从来没有设置画布的大小。minWidth
,maxWidth
,prefWidth
和高度的等效值用于计算大小,而不是设置大小。此外,画布
本身不会调整大小。您需要使用适当的设置器或画布
构造函数设置高度
和宽度
属性。。。