JavaFX 指定路径的球

[java]  view plain copy
  1. package FXExample;  
  2.   
  3. import javafx.animation.PathTransition;  
  4. import javafx.animation.PathTransitionBuilder;  
  5. import javafx.application.Application;  
  6. import javafx.event.ActionEvent;  
  7. import javafx.event.EventHandler;  
  8. import javafx.geometry.Point2D;  
  9. import javafx.scene.Group;  
  10. import javafx.scene.Scene;  
  11. import javafx.scene.SceneBuilder;  
  12. import javafx.scene.input.MouseEvent;  
  13. import javafx.scene.paint.Color;  
  14. import javafx.scene.paint.CycleMethod;  
  15. import javafx.scene.paint.RadialGradient;  
  16. import javafx.scene.paint.Stop;  
  17. import javafx.scene.shape.Circle;  
  18. import javafx.scene.shape.CircleBuilder;  
  19. import javafx.scene.shape.LineTo;  
  20. import javafx.scene.shape.MoveTo;  
  21. import javafx.scene.shape.Path;  
  22. import javafx.stage.Stage;  
  23. import javafx.util.Duration;  
  24.   
  25.   
  26. public class WorkingWithTheSceneGraph extends Application{  
  27.   
  28.     Path onePath = new Path();  
  29.     Point2D anchorPt;//anchor:锚  
  30.     public static void main(String[] args) {  
  31.         launch(args);  
  32.     }  
  33.   
  34.     @Override  
  35.     public void start(Stage primaryStage) throws Exception {  
  36.         primaryStage.setTitle("Chapter 2-3 Working with the Scene Graph");  
  37.         final Group root = new Group();  
  38.         root.getChildren().add(onePath);  
  39.           
  40.         final Scene scene = SceneBuilder.create()  
  41.                 .root(root)  
  42.                 .width(300)  
  43.                 .height(300)  
  44.                 .fill(Color.PINK)  
  45.                 .build();  
  46.         RadialGradient gradient1 = new RadialGradient(0,.1,100,100,20,false,CycleMethod.NO_CYCLE,  
  47.                 new Stop(0,Color.RED),new Stop(1,Color.BLACK));  
  48.           
  49.         //sphere:n. 范围;球体  
  50.         final Circle sphere = CircleBuilder.create()  
  51.                 .centerX(100).centerY(100).radius(20).fill(gradient1).build();  
  52.         root.getChildren().add(sphere);  
  53.           
  54.         // animate sphere by following the path.通过下面的路径动画  
  55.         final PathTransition pathTransition = PathTransitionBuilder.create()  
  56.                 .duration(Duration.millis(4000))//持续时间  
  57.                 .cycleCount(1)//循环周期  
  58.                 .node(sphere)//形状  
  59.                 .path(onePath)//路径  
  60.                 .orientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT)//方向  
  61.                 .build();  
  62.           
  63.         // once finished clear path.结束后清除路径  
  64.         pathTransition.onFinishedProperty().set(new EventHandler<ActionEvent>(){  
  65.   
  66.             @Override  
  67.             public void handle(ActionEvent event) {  
  68.                 onePath.getElements().clear();  
  69.                   
  70.             }});  
  71.           
  72.         // starting initial path:初始路径  
  73.         scene.onMousePressedProperty().set(new EventHandler<MouseEvent>(){  
  74.   
  75.             @Override  
  76.             public void handle(MouseEvent event) {  
  77.                 onePath.getElements().clear();  
  78.                 anchorPt = new Point2D(event.getX(),event.getY());  
  79.                 onePath.setStroke(Color.BLACK);  
  80.                 onePath.setStrokeWidth(3);  
  81.                 onePath.getElements().add(new MoveTo(anchorPt.getX(),anchorPt.getY()));  
  82.                   
  83.             }});  
  84.         // dragging creates lineTos added to the path拖动创建lineTos添加到路径  
  85.         scene.onMouseDraggedProperty().set(new EventHandler<MouseEvent>(){  
  86.   
  87.             @Override  
  88.             public void handle(MouseEvent event) {  
  89.                 onePath.getElements().add(new LineTo(event.getX(),event.getY()));  
  90.             }});  
  91.         scene.onMouseReleasedProperty().set(new EventHandler<MouseEvent>(){  
  92.   
  93.             @Override  
  94.             public void handle(MouseEvent event) {  
  95.                 onePath.setStrokeWidth(10);  
  96.                 if(onePath.getElements().size() > 1){  
  97.                     pathTransition.stop();  
  98.                     pathTransition.playFromStart();  
  99.                 }  
  100.                   
  101.             }});  
  102.           
  103.         primaryStage.setScene(scene);  
  104.         primaryStage.show();  
  105.     }  
  106.   
  107. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值