Question

In: Computer Science

Rectangle Information. This program displays information about a rectangle drawn by the user. Input: Two mouse...

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)

Solutions

Expert Solution

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.


Related Solutions

Write a program, ArrayRange, that asks the user to input integers and that displays the difference...
Write a program, ArrayRange, that asks the user to input integers and that displays the difference between the largest and the smallest. You should ask the user the number of integers s/he would like to enter (this will allow you to set the length of the array). Allow the user to input all the numbers and then store them in the array. Search the array for the largest number, then search for the smallest, and finally compute the calculation. Display...
Write an assembly program that lets the user to input only the word MASM and displays...
Write an assembly program that lets the user to input only the word MASM and displays invalid input for any other user inputs.
Write a program that asks the user for the lengths of the sides of a rectangle....
Write a program that asks the user for the lengths of the sides of a rectangle. Again, check for valid input and exit with an error msg if you don’t get it. Testing: use some known values to confirm that the calculations are correct. E.g. 3 – 4 - 5 triangle >> 3 X 4 rectangle Then print • The area and perimeter of the rectangle • The length of the diagonal (use the Pythagorean theorem). This question should be...
Write a program that gathers input from the user and writes the information out to a...
Write a program that gathers input from the user and writes the information out to a file (output.txt).   Your main method should gather the input, calculate the average, and write the output. You should have a separate method that writes the output to a file. You can have other methods as well if you choose. However, you MUST have at least one other method in addition to the main method. Inputs: Student Number Name Class name Grades 1-5 (5 individual...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Create a program to input the length and width of a rectangle and calculate and print...
Create a program to input the length and width of a rectangle and calculate and print the perimeter and area of the rectangle. To do this you will need to write a Rectangle class and a separate runner class. Your program should include instance variables, constructors, an area method, a perimeter method, a toString method, accessor and mutator methods, and user input. Your runner class should include 3 Rectangle objects. One default rectangle, one coded rectangle, and one user input...
rite a program that will calculate the perimeter and area of a rectangle. Prompt the user...
rite a program that will calculate the perimeter and area of a rectangle. Prompt the user to input the length and width. Calculate the area as length * width. Calculate the perimeter as 2* length + 2* width. Display the area and perimeter. Please use a proper heading in a comment block (name, date, description of the program). Please use comments in your code. Run your program twice using different input. Copy the output and place it at the bottom...
Design a program that will ask the user to input two integer numbers and then perform...
Design a program that will ask the user to input two integer numbers and then perform the basic arithmetic operations such as addition and subtraction. Each calculation is done by a separate function. The main function gets the input from the user, then calls the addition function and the subtraction function one at a time to perform the calculations. Each calculation function (addition or subtraction function) performs an arithmetic operation and then returns the calculation results back to where it...
Question: Conversion Program * This program will ask the user to input meters * After input,...
Question: Conversion Program * This program will ask the user to input meters * After input, a menu will be displayed: * 1. Convert to kilometers * 2. Convert to inches * 3. Convert to feet * 4. Quit * * The user will select the conversion and the converted answer will be displayed. * The program should redisplay the menu for another selection. * Quit program when user selects 4. Hi, my instructor requirement fix the bug in the...
Create a program that keeps track of the following information input by the user: First Name,...
Create a program that keeps track of the following information input by the user: First Name, Last Name, Phone Number, Age Now - let's store this in a multidimensional array that will hold 10 of these contacts. So our multidimensional array will need to be 10 rows and 4 columns. You should be able to add, display and remove contacts in the array. In Java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT