展开全部
图片62616964757a686964616fe78988e69d8331333431346439的位移(下落),可以通过修改图片的x,y坐标来实现, 在Swing/Html中,我们可以使用Timer定时(比如每隔100毫秒)去修改图片的x,y坐标即可实现,
多个图片都按照一定的轨迹移动,那都按照自己的轨迹的算法,去定时修改x,y坐标即可.
JavaFX是java先进的图形界面框架, 里面有3D和各种动画, 所以按照轨迹移动,都能轻松实现
JavaFX参考代码如下
import javafx.animation.Animation;
import javafx.animation.Interpolator;
import javafx.animation.PathTransition;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.QuadCurveTo;
import javafx.stage.Stage;
import javafx.util.Duration;
public class PathAnimateDemo extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
ImageView imv=new ImageView(getClass().getResource("ball.png").toExternalForm());
Path path = new Path();// 路径;运动轨迹
MoveTo mt = new MoveTo(20, 50);
QuadCurveTo quadTo2 = new QuadCurveTo(175, 190, 350, 30);
path.getElements().addAll(mt, quadTo2);
HBox hbox = new HBox(10);
Button btnStart = new Button("开始");
Button btnPause = new Button("暂停");
Button btnResume = new Button("继续");
Button btnStop = new Button("结束");
hbox.getChildren().addAll(btnStart, btnPause, btnResume, btnStop);
hbox.setPadding(new Insets(20));
hbox.setLayoutX(80);
hbox.setLayoutY(230);
Group root = new Group();
root.getChildren().addAll(imv, path, hbox); // 不添加path.就可以不显示path了
Scene scene = new Scene(root, 430, 300);
primaryStage.setTitle("JavaFX");
primaryStage.setScene(scene);
primaryStage.show();
//旋转动画设置
RotateTransition rt=new RotateTransition(Duration.millis(1000),imv);
rt.setInterpolator(Interpolator.LINEAR);
rt.setFromAngle(0);
rt.setToAngle(360);
rt.setCycleCount(Animation.INDEFINITE);
rt.play();
//路径动画设置
PathTransition pt = new PathTransition(Duration.millis(800), path, imv);// 路径动画
pt.setCycleCount(Animation.INDEFINITE);
pt.setAutoReverse(true);
btnStart.setOnAction(e -> {
pt.playFromStart();// 从头开始播放
});
//----按钮的响应设置---
btnPause.setOnAction(e -> {
pt.pause();
});
btnResume.setOnAction(e -> {
pt.play(); // 播放
});
btnStop.setOnAction(e -> {
pt.jumpTo(new Duration(0));// 跳到第0秒处
pt.stop();
});
}
}