package com.example.javafxproject;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.Scanner;
public class TwoRectangle extends Application {
private static double x1, y1, width1, height1;
private static double x2, y2, width2, height2;
public static void main(String[] args) {
try(Scanner input = new Scanner(System.in);) {
System.out.print("Enter center x, y, width, height of rectangle 1: ");
x1 = input.nextDouble(); y1 = input.nextDouble();
width1 = input.nextDouble(); height1 = input.nextDouble();
System.out.print("Enter center x, y, width, height of rectangle 2: ");
x2 = input.nextDouble(); y2 = input.nextDouble();
width2 = input.nextDouble(); height2 = input.nextDouble();
}
launch(args);
}
@Override
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane(getPane());
pane.setBottom(new Text(10, 10, judge()));
BorderPane.setAlignment(pane.getBottom(), Pos.BOTTOM_CENTER);
BorderPane.setMargin(pane.getBottom(), new Insets(10));
Scene scene = new Scene(pane, 500, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("连接两个圆");
primaryStage.show();
}
/**返回面板*/
private Pane getPane() {
Rectangle rectangle1 = new Rectangle(x1, y1, width1, height1); //矩形1
Rectangle rectangle2 = new Rectangle(x2, y2, width2, height2); //矩形2
rectangle1.setStyle("-fx-stroke: black; -fx-fill: null;");
rectangle2.setStyle("-fx-stroke: black; -fx-fill: null;");
return new Pane(rectangle1, rectangle2);
}
/**返回判定描述*/
private String judge() {
double x0 = Math.abs(x2 - x1);
double y0 = Math.abs(y2 - y1);
if ((x0 + width2 / 2 <= width1 / 2) && (y0 + height2 / 2 <= height1 / 2))
return "One rectangle is contained in another";
else if ((width1 - width2) / 2 < x0 && x0 < (width1 + width2) / 2 &&
(height1 - height2) / 2 < y0 && y0 < (height1 + height2) / 2)
return "The rectangle overlap";
else return "The rectangle do not overlap";
}
}