弹球

点击界面随机产生一个弹球

在这里插入图片描述

直接上代码


import java.util.ArrayList;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Test extends Application  {
	public static ObservableList<Node> balls;
	Pane pane=new Pane();
	public static void main(String[] args) {
		launch();
	}
	
    @Override     
	public void start(Stage primaryStage) throws Exception {
    	pane.setPrefSize(800, 800);
    	balls = pane.getChildren();
		pane.setOnMousePressed(e->{
			System.out.println("in");
			Ball ball=new Ball(e.getSceneX(),e.getSceneY());
			balls.add(ball);
			System.out.println(pane.getChildren().size());
		});
		
		BallScheduledService bss=new BallScheduledService(balls);
		bss.setDelay(Duration.seconds(1));
		bss.setPeriod(Duration.seconds(0.1));
		bss.start();
		
		Button cancel=new Button("停止");
		Button restart=new Button("继续");

		HBox hbox=new HBox(20);
		hbox.setAlignment(Pos.CENTER);
		hbox.getChildren().addAll(cancel,restart);
		VBox vbox=new VBox(20);
		vbox.setAlignment(Pos.CENTER);
		vbox.getChildren().addAll(pane,hbox);
		
		Scene scene =new Scene(vbox,800,850);
		primaryStage.setTitle(" *****");
		primaryStage.setScene(scene);
		primaryStage.show();
		cancel.setOnAction(new EventHandler<ActionEvent>(){
			public void handle(ActionEvent event) {
				bss.cancel();
			}
		});
		restart.setOnAction(new EventHandler<ActionEvent>(){
			public void handle(ActionEvent event) {
				bss.restart();
			}
		});

	}


}
class BallScheduledService extends ScheduledService<Number>{
	ObservableList<Node> balls;
	public BallScheduledService(ObservableList<Node> balls) {
		this.balls=balls;
	}
	protected Task<Number> createTask() {
		Task<Number> task=new Task<Number>(){
			protected void updateValue(Number value) {
				super.updateValue(value);
				BallScheduledService.this.balls.forEach(e->{
					Ball ball=(Ball)e;
					ball.moveBall();
				});
			}

			protected Number call() throws Exception {
				return 0;
			}
			
		};
		return task;
	}
	
}
class Ball extends Circle {
	int n=10;
	static int count=0;
	public  double dx=3,dy=3,dv=10;
	public Ball(double x,double y) {
		super();
		this.setCenterX(x);
		this.setCenterY(y);
		this.setRadius(	Math.random()*20+8);
		dx=(Math.random()>0.5?1:-1)*(Math.random()*dv+1);
		dy=(Math.random()>0.5?1:-1)*(Math.random()*dv+1);
		System.out.println("dx :"+dx);
		System.out.println("dy :"+dy);
		this.setFill(Color.color(Math.random()*0.95+0.05, Math.random()*0.95+0.05, Math.random()*0.95+0.05));
	}
	public void moveBall() {
		double R=this.getRadius();
		double x=this.getCenterX(),y=this.getCenterY();
		if(x<R){
		    dx=Math.abs(dx);
    	}else if(x>800-R) {
    		dx=-Math.abs(dx);
    	}
		if(y<R){
		    dy=Math.abs(dy);
    	}else if(y>800-R) {
    		dy=-Math.abs(dy);
    	}
		x+=dx;
		y+=dy;
		this.setCenterX(x);
		this.setCenterY(y);
	}
}


猜你喜欢

转载自blog.csdn.net/qq_39464369/article/details/88602382