【发布时间】:2022-01-26 20:35:51
【问题描述】:
我正在尝试创建 PacMan 游戏,但我正在尝试使用 platform.runLater() 来实现多线程并且 GUI 没有更新,即使我已经检查过代码本身正在运行。以下是相关代码:
这是启动 Platform.runLater() 线程的代码块。
private void startTimer() {
this.timer = new java.util.Timer();
TimerTask timerTask = new TimerTask() {
public void run() {
Platform.runLater(new Runnable() {
public void run() {
update();
}
});
}
};
long frameTimeInMilliseconds = (long)(1000.0 / FRAMES_PER_SECOND);
this.timer.schedule(timerTask, 0, frameTimeInMilliseconds);
}
private void update() {
view.update(this.modelo);
}
这是它正在运行的代码块。
public void update(Model gc) {
updateMap(gc);
}
//Sets the image on the already initialized imageviews (not updating gui for some reason)
private void updateMap(Model gc) {
for(int i = 0; i < rowCount; i++) {
for(int j = 0; j < columnCount; j++) {
char valor = gc.getCelulas(i, j);
if(valor == 'E' || valor == 'W') {
visaoMapa[i][j].setImage(imgsMapaVazio[i][j]);
}
else {
if(valor == 'S')
visaoMapa[i][j].setImage(imgsMapaCheio[i][j]);
}
}
}
}
更新 #1 按照 James_D 的建议,我尝试将 startTimer 方法更改为:
private void startTimer() {
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
update();
}
};
timer.start();
}
但是,问题仍然存在。
【问题讨论】:
-
这很可能完全是错误的方法。考虑使用动画 API 等。一个
AnimationTimer。 -
对于example。
标签: multithreading javafx