Java语言程序设计基础篇_编程练习题**15.31(几何问題:钟摆)

**15.31(几何问題:钟摆)

编写一个程序,用动画完成钟摆,如图15-35所示。单击向上箭头UP键增加速度,单击向下箭头键DWON降低速度。单击S键停止动画,单击R键重新开始

  • 习题思路:
  1. 创建两个圆Circle,第二个圆比第一个圆稍大一点,作为悬挂点和摆动点
  2. 新建一条直线,开始点是圆1的中心点,结束点是圆2的中心点
  3. 创建两个Arc作为摆动点的路径和直线的路径,第二个Arc的长与高是第一个的两倍
  4. 创建两个PathTransition对象,第一个对象的路径设为arc1,节点为circle2.第二个对象的路径设为arc2,节点为line
  5. 为Scene注册一个键盘事件,单击上下箭头则把动画的速率+0.1或-0.1,单击S或R则暂停或开始动画

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

package chapter_15;

import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;

public class 编程练习题15_31Pendulum extends Application{
	@Override
	public void start(Stage primaryStage) throws Exception {
		Pane pane = new Pane();
		Scene scene = new Scene(pane, 300, 300);
		// 创建小球c1 (悬挂点)
        Circle c1 = new Circle(400, 20, 10);

        // 创建小球c2 (摆动点)
        Circle c2 = new Circle(400, 320, 20);

        // 创建线
        Line line = new Line(c1.getCenterX(), c1.getCenterY(), c2.getCenterX(), c2.getCenterY());

        // 创建摆动路径
        Arc arc = new Arc(400, 20, 300, 300, -45, -90);
        arc.setType(ArcType.OPEN);
        arc.setFill(Color.WHITE);
        arc.setStroke(Color.BLACK);

        Arc arc2 = new Arc(400, 20, 150, 150,-45, -90);
        arc2.setType(ArcType.OPEN);
        arc2.setFill(Color.WHITE);
        arc2.setStroke(Color.BLACK);
        pane.getChildren().addAll(c1, c2, line);

        // 创建PathTransition动画
        PathTransition pt = new PathTransition();
        pt.setDuration(Duration.millis(2000)); // 动画持续时间
        pt.setPath(arc); // 动画路径
        pt.setNode(c2); // 要移动的对象
        pt.setAutoReverse(true); // 自动反向播放
        pt.setCycleCount(Timeline.INDEFINITE); // 无限循环
        pt.play(); // 开始动画
        
        PathTransition pt2 = new PathTransition();
        pt2.setDuration(Duration.millis(2000)); // 动画持续时间
        pt2.setPath(arc2); // 动画路径
        pt2.setNode(line); // 要移动的对象
        pt2.setAutoReverse(true); // 自动反向播放
        pt2.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
        pt2.setCycleCount(Timeline.INDEFINITE); // 无限循环
        pt2.play(); // 开始动画
        
		primaryStage.setTitle("编程练习题15_31Pendulum");
		primaryStage.setScene(scene);
		primaryStage.show();
		
		scene.setOnKeyPressed(e->{
			if(e.getCode() == KeyCode.UP) {
				pt.setRate(pt.getRate()+0.1);
				pt2.setRate(pt2.getRate()+0.1);
			}else if(e.getCode() == KeyCode.DOWN) {
				pt.setRate(pt.getRate()>0.1?pt.getRate()-0.1:0);
				pt2.setRate(pt2.getRate()>0?pt2.getRate()-0.1:0);
			}
			if(e.getCode() == KeyCode.S) {
				pt.pause();
				pt2.pause();
			}
			if(e.getCode() == KeyCode.R) {
				pt.play();
				pt2.play();
			}
		});
		
	}
	public static void main(String[] args) {
		Application.launch(args);
	}
}
  •  结果示例

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值