In: Computer Science
Rectangle Information. This program displays information about a rectangle drawn by the user. Input: Two mouse clicks for the opposite comers of a rectangle. Output: Draw the rectangle. Print the perimeter and area of the rectangle. Formulas: area = (length)(width) perimeter = 2(length + width)

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
class DrawRectangles extends JComponent {
        private Image image;
        private Graphics2D g2;
        private int currentX, currentY, oldX, oldY;
        public DrawRectangles() {
                setDoubleBuffered(false);
                addMouseListener(new MouseAdapter() {
                        public void mousePressed(MouseEvent e) {
                                // save coord x,y when mouse is pressed
                                oldX = e.getX();
                                oldY = e.getY();
                        }
                        public void mouseReleased(MouseEvent e) {
                                // save coord x,y when mouse is pressed
                                currentX = e.getX();
                                currentY = e.getY();
                                int width = Math.abs(currentX - oldX);
                                int height = Math.abs(currentY - oldY);
                                System.out.println("Area: " + (width * height) + "sq pixels");
                                System.out.println("Perimeter: " + 2*(width + height) + "pixels");
                                System.out.println();
                                
                                int leftX = Math.min(currentX, oldX);
                                int leftY = Math.min(currentY, oldY);
                                g2.drawRect(leftX, leftY, width, height);
                                g2.fillRect(leftX, leftY, width, height);
                                repaint();
                                
                        }
                });
        }
        protected void paintComponent(Graphics g) {
                if (image == null) {
                        // image to draw null ==> we create
                        image = createImage(getSize().width, getSize().height);
                        g2 = (Graphics2D) image.getGraphics();
                        // clear draw area
                        clear();
                }
                g.drawImage(image, 0, 0, null);
                repaint();
        }
        // now we create exposed methods
        public void clear() {
                g2.clearRect(0, 0, getSize().width, getSize().height);
                repaint();
        }
}
public class Rectangles {
        DrawRectangles paintGUI;
        public static void main(String[] args) {
                new Rectangles().show();
        }
        public void show() {
                // create main frame
                JFrame frame = new JFrame("Rectangle Paint");
                Container content = frame.getContentPane();
                // set layout on content pane
                content.setLayout(new BorderLayout());
                // create draw area
                paintGUI = new DrawRectangles();
                // add to content pane
                content.add(paintGUI, BorderLayout.CENTER);
                frame.setSize(800, 800);
                // can close frame
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                // show the swing paint result
                frame.setVisible(true);
        }
}
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.