javafx小球运动

javafx使用图形界面在随机位置生成颜色随机、半径随机、初始运动速度随机的小球,然后按照某种轨迹进行运动(运动轨迹方程自选),到边界碰撞返回。

我太爱我的java张春凤老师了,第一次写博客记录编程生涯,新人还请多多关照!

根据大佬内容改编: link.

package application;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import java.util.Timer;
import java.util.TimerTask;

public class MyWork extends Application {
    
    
	static int x = 200;
	static int y = 90;
	static double distance_x = Math.random() * 4 + 1;
	static double distance_y = Math.sqrt(25 - distance_x * distance_x);
	static Ellipse e = new Ellipse();
	static String temp1 = "left";
	static String temp2 = "down";

	// 只有第一次点击有效
	static Timer t = new Timer();
	static boolean record_start = true;
	static boolean record_stop = false;

	public static void main(String[] args) {
    
    
		launch(args);
	}

	public void start(Stage s) {
    
    

		Group root = new Group();
		Scene scene = new Scene(root, 400, 250, Color.WHITE);
		Button start = new Button("开始");
		Button stop = new Button("停止");

		// 分割线
		Line l = new Line();
		l.setStartX(0);
		l.setStartY(160);
		l.setEndY(160);
		l.setEndX(400);

		// 放置小球
		e.setCenterX((Math.random() * 400) + 1);
		e.setCenterY((Math.random() * 60) + 1);
		e.setRadiusX((Math.random() * 2) + 10);
		e.setRadiusY((Math.random() * 2) + 10);
		e.setFill(Color.rgb((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)));
		start.setLayoutX(100);
		start.setLayoutY(190);
		stop.setLayoutX(250);
		stop.setLayoutY(190);
		start.setOnAction(event -> {
    
    

			if (record_start) {
    
    
				t = new Timer();
				t.schedule(new TimerTask() {
    
    
					public void run() {
    
    

						if (x < 20) {
    
    
							temp1 = "right";
						}
						if (x > 380) {
    
    
							temp1 = "left";
						}

						if (y < 20) {
    
    
							temp2 = "up";
						}
						if (y > 140) {
    
    
							temp2 = "down";
						}
						if (temp1.equals("left")) {
    
    
							e.setCenterX(x -= distance_x);
						} else {
    
    
							e.setCenterX(x += distance_x);
						}
						if (temp2.equals("down")) {
    
    
							e.setCenterY(y -= distance_y);
						} else {
    
    
							e.setCenterY(y += distance_y);
						}
					}
				}, 0, 20);
			}
			// “开始"按钮被点击且事件被触发,record=false;
			record_start = false;
			record_stop = false;
		});

		stop.setOnAction(event -> {
    
    
			System.out.println("停止反弹");
			// 当第二次点击"停止"时,小球重置
			if (record_stop) {
    
    

				distance_x = Math.random() * 4 + 1;
				distance_y = Math.sqrt(25 - distance_x * distance_x);

				e.setCenterX((Math.random() * 400) + 1);
				e.setCenterY((Math.random() * 60) + 1);
				record_stop = false;
			}
			record_stop = true;
			record_start = true;
			t.cancel();
		});

		root.getChildren().addAll(start, stop, l, e);
		s.setTitle("小球反弹");
		s.setScene(scene);
		s.show();
	}
}

写的不是很好,再接再厉好好学习!嘻嘻

猜你喜欢

转载自blog.csdn.net/CHIhacker666/article/details/108803957