JAVA动画不显示怎么办,JavaFX - 时间轴动画对象不显示?
我确定'm missing something obvious here. I'我试图制作一辆汽车的动画,它从左到右穿过一扇窗户,当它到达那里时绕着右侧缠绕 . 用户应该能够按上/下来调整动画的速度 . 当我使用 PathTransition 对象时,我有动画,但发现你无法调整 PathTransition 的 Duration ,所以我在 Timeline 中重新编写它 .
但是,对于 Timeline ,我希望是一个简洁的代码片段:
public class Project12 extends Application {
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage primaryStage) {
//Create Pane to hold the car animation
Pane pane = new Pane();
//Create the RaceCarPane
RaceCarPane raceCar = new RaceCarPane();
pane.getChildren().add(raceCar); //Adds the race car to the main pane
//Create the VBox to hold components
VBox displayPane = new VBox();
displayPane.getChildren().addAll(pane, userInstructions, btnPause);
displayPane.setSpacing(15);
displayPane.setAlignment(Pos.CENTER);
//Create scene for display and add the display pane
Scene scene = new Scene(displayPane);
//Add the scene to the stage and display
primaryStage.setTitle("Project 12");
primaryStage.setResizable(false); //disable resizing of the window
primaryStage.setScene(scene);
primaryStage.show();
}
而RaceCarPane:
public class RaceCarPane extends Pane {
//Declare origin for determining polygon point locations
private double originX = 10;
private double originY = getHeight() - 10;
private Timeline carAnimation;
//Set the Timeline for the car in constructor method
public RaceCarPane() {
carAnimation = new Timeline(
new KeyFrame(Duration.millis(100), e -> moveCar()));
carAnimation.setCycleCount(Timeline.INDEFINITE);
carAnimation.play();
}
private void paint() {
//Create a polygon for the body
Polygon body = new Polygon();
body.setFill(Color.BLUE);
body.setStroke(Color.DARKBLUE);
//Add points to the body
ObservableList bodyList = body.getPoints();
/*(code omitted, just adding coordinates to the ObservableList for all parts.
I don't believe the bug is here since it displayed when I was using a PathTransition animation)*/
//Add to pane
getChildren().addAll(body, roof, frontWheel, rearWheel);
}
public void setOrigin (double x, double y) {
this.originX = x;
this.originY = y;
}
@Override
public void setWidth(double width) {
super.setWidth(width);
paint();
}
@Override
public void setHeight(double height) {
super.setHeight(height);;
paint();
}
public void moveCar() {
//Check that car is in bounds
if(originX <= getWidth()) {
originX += 10;
paint();
}
else {
originX = 0;
paint();
}
编辑:Per @Jai的评论如下,我的解决方案是恢复到 PathTransition 对象并使用 RateProperty 绑定到 SimpleDoubleProperty . 虽然可能不是该项目所寻求的解决方案,但它可以解决问题,所以我很开心!
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
