Java语言程序设计基础篇_编程练习题**15.25 (动画:曲线上的球)

**15.25 (动画:曲线上的球)

请编写一个程序,用动画实现一个沿着正弦函数曲线移动的球,如图15-32所示。当球到达右边界时,它从左边重新开始。用户可以单击鼠标左/右按钮来继续/暂停动画

  • 习题思路:
  1.  用一个Polyline绘制sin曲线,y随着x的变化而变化,公式及代码可以参考书上的编程练习题14.19
  2. 绘制坐标轴以及图例
  3. 新建一个小圆Cirlce,创建PathTransition对象,把路径设置为sin曲线Polyline,节点设为小圆Circle
  4. 为面板注册一个鼠标事件(鼠标点击:setOnMouseClicked()),左键被点击调用pause()方法,右键点击调用play()方法

代码示例: 编程练习题15_25ballOnSinCurve.java

package chapter_15;

import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polyline;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class 编程练习题15_25ballOnSinCurve extends Application{
	@Override
	public void start(Stage primaryStage) throws Exception {
		Pane pane = new Pane();
		Polyline polyline = new Polyline();
		polyline.setStroke(Color.RED);;
		pane.getChildren().add(polyline);
		ObservableList<Double> list = polyline.getPoints();
		double scaleFactor = 50;
		for(int x = -170;x <= 170;x++) {
			list.add(x + 200.0);
			list.add(100 - scaleFactor * Math.sin((x / 100.0)*2*Math.PI));
		}
		
		Polyline p3 = new Polyline(200, 200,  200, 20,  210, 30,  200, 20,  190, 30);
		pane.getChildren().add(p3);
		Polyline p4 = new Polyline(20,100, 380,100,  370,110 , 380,100, 370, 90);
		pane.getChildren().add(p4);
		
		
		Text x = new Text(370, 70,"X");
		Text y = new Text(220, 20, "Y");
		pane.getChildren().add(x);
		pane.getChildren().add(y);
		
		Text o = new Text(205, 115, "0");
		
		Text t1 = new Text(50, 110, "-3\u03c0");
		Text t2 = new Text(100, 110, "-2\u03c0");
		Text t3 = new Text(150, 110, "-\u03c0");
		Text t4 = new Text(250, 110, "\u03c0");
		Text t5 = new Text(300, 110, "2\u03c0");
		Text t6 = new Text(350, 110, "3\u03c0");
		
		
		pane.getChildren().addAll(o,t1,t2,t3,t4,t5,t6);
		Circle circle = new Circle(10);
		circle.setFill(Color.ORANGE);
		pane.getChildren().add(circle);
		
		PathTransition pt = new PathTransition();
		pt.setDuration(Duration.millis(5000));
		pt.setPath(polyline);
		pt.setNode(circle);
		pt.setCycleCount(Timeline.INDEFINITE);
		pt.play();
		
		pane.setOnMouseClicked(e -> {
			if(e.getButton() == MouseButton.PRIMARY) {
				pt.pause();
			}else if(e.getButton() == MouseButton.SECONDARY){
				pt.play();
			}
		});
		
		
		Scene scene = new Scene(pane, 400, 200);
		primaryStage.setTitle("编程练习题15_25ballOnSinCurve");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	public static void main(String[] args) {
		Application.launch(args);
	}
}
  •  结果展示:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值