package com.example.javafxproject;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Polyline;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.linkedList;
import java.util.Scanner;
public class PolygonChart extends Application {
private static linkedList list = new linkedList<>();
public static void main(String[] args) {
try(Scanner input = new Scanner(System.in);) {
System.out.print("Enter five points: ");
for (int i = 0; i < 10; i++)
list.addLast(input.nextDouble());
}
launch(args);
}
// 10 10 50 19 100 100 8 90 50 50
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(getPane(), 500, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("在一个多边形上吗?");
primaryStage.show();
}
private Pane getPane() {
BorderPane pane = new BorderPane();
Polygon polygon = new Polygon();
ObservableList observableList = polygon.getPoints(); //获取多边形的可观察列表并赋值引用
while (list.size() > 2) //添加前4个点进入列表
observableList.addAll(list.removeFirst(), list.removeFirst());
Circle point = new Circle(list.removeFirst(), list.removeFirst(), 10); //使用第5个点创建圆
polygon.setStyle("-fx-fill: null; -fx-stroke: black;");
pane.setCenter(new Group(polygon, point));
pane.setBottom(new Text(10, 10, "The point is " + (polygon.contains(point.getCenterX(),
point.getCenterY()) ? "" : "not ") + "inside the polygon"));
BorderPane.setAlignment(pane.getBottom(), Pos.CENTER);
BorderPane.setMargin(pane.getBottom(), new Insets(10));
return pane;
}
}