JavaFX-如何每两秒钟刷新一次场景
嗨,我想用JavaFx创建一个简单的游戏,但我有问题。我用Backgound和人物图像创建场景 该源将3张人物图像随机放置在背景上。 不幸的是,我不能使图像移动 现在我希望每2秒钟刷新一次场景,让人们在新的地方找到新的位置JavaFX-如何每两秒钟刷新一次场景,java,netbeans,javafx,Java,Netbeans,Javafx,嗨,我想用JavaFx创建一个简单的游戏,但我有问题。我用Backgound和人物图像创建场景 该源将3张人物图像随机放置在背景上。 不幸的是,我不能使图像移动 现在我希望每2秒钟刷新一次场景,让人们在新的地方找到新的位置 package Game; import java.util.Random; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventH
package Game;
import java.util.Random;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.BoxBlur;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Game extends Application {
final static int h = 775;
final static int w = 523;
@Override
public void start(Stage primaryStage) throws InterruptedException {
Game x = new Game();
Group root = new Group(x.background, x.persons);
x.pokaz_tlo.setEffect(new BoxBlur());
x.pokaz_tlo.setOpacity(0.9);
primaryStage.setTitle("Good game");
primaryStage.setScene(new Scene(root, h, w));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class Game{
final static Image background = new Image(Game.class.getResource("background.jpg").toString());
final static Image person1 = new Image(Game.class.getResource("person1.jpg").toString());
final static Image person2 = new Image(Game.class.getResource("person2.jpg").toString());
final static Image person3 = new Image(Game.class.getResource("person3.jpg").toString());
ImageView show_background = new ImageView(background);
ImageView show_person1 = new ImageView(person1);
ImageView show_person2 = new ImageView(person2);
ImageView show_person3 = new ImageView(person3);
Group persons = pokaz_ukrywaj_ludzi(show_person1, show_person2, show_person3);
public int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public Group pokaz_ukrywaj_ludzi(ImageView person1, ImageView person2, ImageView person3) {
final int[] x = {164, 320, 475};
final int[] y = {75, 270};
person1.setTranslateX(x[randInt(0,2)]);
person1.setTranslateY(y[randInt(0,1)]);
person2.setTranslateX(x[randInt(0,2)]);
person2.setTranslateY(y[randInt(0,1)]);
person3.setTranslateX(x[randInt(0,2)]);
person3.setTranslateY(y[randInt(0,1)]);
Group l = new Group(person1, person2, person3);
return l;
}
}
有什么想法吗?现在问题来了:
在JavaFX中,没有像Swing中那样的repaint()或validate()的空白
但是您可以编写自己的void来刷新整个窗口
每2秒:
new Timer().scheduleAtFixedRate(new TimerTask() {
public void run() {
// Here comes your void to refresh the whole application.
}
}, 2000, 2000);
现在问题来了:
在JavaFX中,没有像Swing中那样的repaint()或validate()的空白
但是您可以编写自己的void来刷新整个窗口
每2秒:
new Timer().scheduleAtFixedRate(new TimerTask() {
public void run() {
// Here comes your void to refresh the whole application.
}
}, 2000, 2000);
您需要使用JavaFX动画计时器
您需要使用JavaFX动画计时器
我不在JavaFX中工作。但我知道的是,您应该使用计时器来刷新场景,因为您的实现覆盖了包含游戏视觉效果的组件的绘制方法,所以会刷新场景。在计时器中,它应该具有游戏逻辑的更新和修改的绘制结果。我在游戏中使用了java.util.Timer,但我不是专业人士,它适合我的解决方案。我不在JavaFX中工作。但我知道的是,您应该使用计时器来刷新场景,因为您的实现覆盖了包含游戏视觉效果的组件的绘制方法,所以会刷新场景。在计时器中,它应该具有游戏逻辑的更新和修改的绘制结果。我在游戏中使用了java.util.Timer,但我不是专业人士,它适合我的解决方案。