import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
public class Exercise14_22 extends Application{
public static void main(String[] args){
launch(args);
}
public void start (Stage stage){
double centerX1 = Math.random() * 400,centerY1 = Math.random() * 400;
double centerX2 = Math.random() * 400,centerY2 = Math.random() * 400;//产生随机数
double radius = 15;
Pane pane = new Pane();
Circle circle1 = new Circle();//画出第一个圆
circle1.setCenterX(centerX1);
circle1.setCenterY(centerY1);
circle1.setRadius(radius);
circle1.setFill(Color.WHITE);
circle1.setStroke(Color.BLACK);
Circle circle2 = new Circle();//画出第二个圆
circle2.setCenterX(centerX2);
circle2.setCenterY(centerY2);
circle2.setRadius(15);
circle2.setFill(Color.WHITE);
circle2.setStroke(Color.BLACK);
Line line = new Line();
double n = Math.atan(Math.abs(centerY1 - centerY2)/Math.abs(centerX1 - centerX2));//以下为运用数学知识计算
double X1,X2,Y1,Y2;
if(centerX1 >= centerX2){
X1 = centerX1 - radius * Math.cos(n);
X2 = centerX2 + radius * Math.cos(n);
}else {
X1 = centerX1 + radius * Math.cos(n);
X2 = centerX2 - radius * Math.cos(n);
}
if(centerY1 >= centerY2){
Y1 = centerY1 - radius * Math.sin(n);
Y2 = centerY2 + radius * Math.sin(n);
}else {
Y1 = centerY1 + radius * Math.sin(n);
Y2 = centerY2 - radius * Math.sin(n);
}
line.setStartX(X1);
line.setStartY(Y1);
line.setEndX(X2);
line.setEndY(Y2);
pane.getChildren().addAll(circle1,circle2,line);
Scene scene = new Scene(pane,400,400);
stage.setTitle("Exercise14_22");
stage.setScene(scene);
stage.show();
}
}
随机产生两个圆,用一条直线连接两圆,直线不能穿到圆内(javaFx)
最新推荐文章于 2024-08-24 07:52:35 发布