JavaFX、两个矩形

 


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";
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值