JavaFX动画缺少什么方面



我正试图让这个简单的动画在图像背景上播放,但我无法启动它。我尝试添加一个按钮,并使用playFromStart((代替play((。我还试着在路径上添加设定的方向,我认为这不会有任何作用,因为我只是在移动一个圆圈,但没有帮助。我还试着打乱动画的时间和重复次数,以防一切都发生得很快或很慢,而我只是错过了。我觉得我可能错过了一些非常简单的东西,但从我所看到的一切,所有的例子中,我也有。

当我添加按钮时,背景图像也消失了,为此我尝试过将其向上移动和其他事情,但我觉得这也是一个简单的问题,我的大脑刚刚被它淹没了。

package javafxapplication10;
import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.util.Duration;

public class JavaFXApplication10 extends Application {
@Override
public void start(Stage stage) {

stage.setScene(scene);
stage.setResizable(false);
stage.sizeToScene();
ImagePattern pattern = new ImagePattern(image);
scene.setFill(pattern);        
stage.setScene(scene);
stage.show();
Circle cir = new Circle (19);            
cir.setLayoutX(170);
cir.setLayoutY(100);
cir.setFill(Color.KHAKI);
pane.getChildren().add(cir);
Path path1 = new Path();

path1.getElements().add(new MoveTo(170,650));
path1.getElements().add(new MoveTo(1335,650));
path1.getElements().add(new MoveTo(1335,100));
PathTransition pl = new PathTransition();
pl.setDuration(Duration.seconds(8));
pl.setPath(path1);
pl.setNode(cir);
pl.setCycleCount(1);
//pl.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
pl.setAutoReverse(false);
//pl.play();
Button begin = new Button("Begin");
begin.setLayoutX(780);
begin.setLayoutY(105);
begin.setOnAction(new EventHandler<ActionEvent> () {
@Override
public void handle(ActionEvent press) {
pl.play();
}
}); 
pane.getChildren().add(begin);

}
Image image = new Image("file:Figure one.png");
Pane pane = new Pane();
Scene scene = new Scene (pane,1474,707);
public static void main(String[] args) {
launch(args);
}
}

PathTransition仅沿实际绘制的路径移动节点。MoveTo元素不绘制任何内容,只是简单地设置当前位置。您需要使用LineTo(和/或ClosePath(在Path中绘制一些内容。此外,PathTransition设置平移poperties,而不是布局属性,即通过将布局坐标添加到Path提供的坐标来确定圆的最终位置。因此,您应该使用translate属性定位Circle,或者在(0, 0):处启动路径

Path path1 = new Path(
new MoveTo(0, 0),
new LineTo(0, 550),
new LineTo(1165, 550),
new LineTo(1165, 0),
new ClosePath()
);
// path1.getElements().add(new MoveTo(170,650));
// path1.getElements().add(new MoveTo(1335,650));
// path1.getElements().add(new MoveTo(1335,100));

最新更新