Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Geometry: two rectangles) Write a program that prompts the user to enter the ce

ID: 3566123 • Letter: #

Question

(Geometry: two rectangles) Write a program that prompts the user to enter the
center coordinates, width, and height of two rectangles from the command line.
The program displays the rectangles and a text indicating whether the two are
overlapping, whether one is contained in the other, or whether they dont overlap,
as shown in Figure 14.50. See Programming Exercise 10.13 for checking the
relationship between two rectangles.

FIGURE 14.50

(Geometry: two rectangles) Write a program that prompts the user to enter the center coordinates, width, and height of two rectangles from the command line. The program displays the rectangles and a text indicating whether the two are overlapping, whether one is contained in the other, or whether they don½t overlap, as shown in Figure 14.50. See Programming Exercise 10.13 for checking the relationship between two rectangles. FIGURE 14.50

Explanation / Answer

import java.awt.Color;
import java.awt.Graphics;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class TwoRectangles extends JPanel{

   static int x1,x2,y1,y2,w1,w2,h1,h2;


   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);
       System.out.println("Enter Details for Rectangle 1... ");
       System.out.print("Center Co-ordinate 'X': ");
       x1 = in.nextInt();
       System.out.print("Center Co-ordinate 'Y': ");
       y1 = in.nextInt();
       System.out.print("Width: ");
       w1 = in.nextInt();
       System.out.print("Height: ");
       h1 = in.nextInt();

       System.out.println("Enter Details for Rectangle 2... ");
       System.out.print("Center Co-ordinate 'X': ");
       x2 = in.nextInt();
       System.out.print("Center Co-ordinate 'Y': ");
       y2 = in.nextInt();
       System.out.print("Width: ");
       w2 = in.nextInt();
       System.out.print("Height: ");
       h2 = in.nextInt();

       in.close();

       JFrame window = new JFrame("Exercise 14_23");
       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       window.setSize(300, 300);
       window.getContentPane().setLayout(null);

       String message;

       if(x2> x1+w1 && y2> y1+h1){
           message = "The Rectangles do not overlap";
       }
       else if(x1> x2+w2 && y1> y2+h2){
           message = "The Rectangles do not overlap";
       }
       else if(x2>x1 && x2+w2<x1+w1){
           message = "One Rectangle is contained in another";
       }
       else if(x1>x2 && x1+w1<x2+w2){
           message = "One Rectangle is contained in another";
       }
       else{
           message = "The Rectangles overlap";
       }
       JPanel panel = new TwoRectangles();
       panel.setBounds(10, 10, 265, 210);
       panel.setBackground(Color.WHITE);
       window.getContentPane().add(panel);
       JLabel label = new JLabel(message);
       label.setBounds(10, 230, 265, 20);
       window.getContentPane().add(label);
       window.setVisible(true);


   }

   public void paint(Graphics g){
       g.drawRect(x1, y1, w1, h1);
       g.drawRect(x2, y2, w2, h2);
   }

}