package com.example.javafxproject;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.security.SecureRandom;
public class TwoCircle extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(getPane(), 500, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("连接两个圆");
primaryStage.show();
}
/**返回面板*/
private Pane getPane() {
Circle circle1 = new Circle(getRandomPoint(), getRandomPoint(), 15); //圆1
Circle circle2 = new Circle(getRandomPoint(), getRandomPoint(), 15); //圆2
Line line = new Line(circle1.getCenterX(), circle1.getCenterY(), circle2.getCenterX(), circle2.getCenterY());
circle1.setStyle("-fx-stroke: black; -fx-fill: white;");
circle2.setStyle("-fx-stroke: black; -fx-fill: white;");
return new Pane(line, circle1, circle2, new Text(circle1.getCenterX(), circle1.getCenterY(), "1"),
new Text(circle2.getCenterX(), circle2.getCenterY(), "2"));
}
/**返回随机点*/
private double getRandomPoint() {
return 10 + new SecureRandom().nextDouble(300 - 10);
}
}