JavaFX 指定路径的球

package FXExample;

import javafx.animation.PathTransition;
import javafx.animation.PathTransitionBuilder;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;
import javafx.scene.shape.CircleBuilder;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;
import javafx.util.Duration;


public class WorkingWithTheSceneGraph extends Application{

	Path onePath = new Path();
	Point2D anchorPt;//anchor:锚
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("Chapter 2-3 Working with the Scene Graph");
		final Group root = new Group();
		root.getChildren().add(onePath);
		
		final Scene scene = SceneBuilder.create()
				.root(root)
				.width(300)
				.height(300)
				.fill(Color.PINK)
				.build();
		RadialGradient gradient1 = new RadialGradient(0,.1,100,100,20,false,CycleMethod.NO_CYCLE,
				new Stop(0,Color.RED),new Stop(1,Color.BLACK));
		
		//sphere:n. 范围;球体
		final Circle sphere = CircleBuilder.create()
				.centerX(100).centerY(100).radius(20).fill(gradient1).build();
		root.getChildren().add(sphere);
		
		// animate sphere by following the path.通过下面的路径动画
		final PathTransition pathTransition = PathTransitionBuilder.create()
				.duration(Duration.millis(4000))//持续时间
				.cycleCount(1)//循环周期
				.node(sphere)//形状
				.path(onePath)//路径
				.orientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT)//方向
				.build();
		
		// once finished clear path.结束后清除路径
		pathTransition.onFinishedProperty().set(new EventHandler<ActionEvent>(){

			@Override
			public void handle(ActionEvent event) {
				onePath.getElements().clear();
				
			}});
		
		// starting initial path:初始路径
		scene.onMousePressedProperty().set(new EventHandler<MouseEvent>(){

			@Override
			public void handle(MouseEvent event) {
				onePath.getElements().clear();
				anchorPt = new Point2D(event.getX(),event.getY());
				onePath.setStroke(Color.BLACK);
				onePath.setStrokeWidth(3);
				onePath.getElements().add(new MoveTo(anchorPt.getX(),anchorPt.getY()));
				
			}});
		// dragging creates lineTos added to the path拖动创建lineTos添加到路径
		scene.onMouseDraggedProperty().set(new EventHandler<MouseEvent>(){

			@Override
			public void handle(MouseEvent event) {
				onePath.getElements().add(new LineTo(event.getX(),event.getY()));
			}});
		scene.onMouseReleasedProperty().set(new EventHandler<MouseEvent>(){

			@Override
			public void handle(MouseEvent event) {
				onePath.setStrokeWidth(10);
				if(onePath.getElements().size() > 1){
					pathTransition.stop();
					pathTransition.playFromStart();
				}
				
			}});
		
		primaryStage.setScene(scene);
        primaryStage.show();
	}

}


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值