Question

In: Computer Science

(Display a Circle and Its Attributes) Write a program that displays a circle of random size...

(Display a Circle and Its Attributes) Write a program that displays a circle of random size and calculates and displays the area, radius, diameter and circumference. Use the following equations: diameter = 2 × radius, area = π × radius2, circumference = 2 × π × radius. Use the constant Math.PI for pi (π). All drawing should be done on a subclass of JPanel, and the results of the calculations should be displayed in a read-only JTextArea.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

//RandomCircle.java

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;

public class RandomCircle extends JFrame {

      // panel to draw a circle (see CirclePanel class at the bottom)

      private CirclePanel circle;

      // text area to represent stats

      private JTextArea stats;

      // constructor to initialize the GUI

      public RandomCircle() {

            // will exit on close

            setDefaultCloseOperation(EXIT_ON_CLOSE);

            // initializing circle panel, with a random radius between 50 and 200

            circle = new CirclePanel((int) (Math.random() * 151) + 50);

            // and the read only text area

            stats = new JTextArea();

            stats.setEditable(false);

            // finding area, circumference and diameter of circle

            double area = Math.PI * circle.getRadius() * circle.getRadius();

            double circumference = 2 * Math.PI * circle.getRadius();

            int diameter = circle.getRadius() * 2;

            // updating stats text area withg all these info

            stats.setText(String.format(

                        "Area: %.2f\nRadius: %d\nDiameter: %d\nCircumference: %.2f",

                        area, circle.getRadius(), diameter, circumference));

            // adding circle panel to the center and stats to the bottom

            add(circle, BorderLayout.CENTER);

            add(stats, BorderLayout.PAGE_END);

            // using 500x500 size

            setSize(500, 500);

            // making window visible

            setVisible(true);

      }

      public static void main(String[] args) {

            // initializing GUI

            new RandomCircle();

      }

}

// CirclePanel class to draw a circle. should be placed in the same file

class CirclePanel extends JPanel {

      // radius of circle

      private int radius;

      // constructor taking circle radius

      public CirclePanel(int radius) {

            this.radius = radius;

      }

      // returns the radius

      public int getRadius() {

            return radius;

      }

      // method to draw the circle whenever panel is updated

      @Override

      protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            // using gray color

            g.setColor(Color.GRAY);

            // drawing a filled circle centered at the center of the panel

            g.fillOval(getWidth() / 2 - radius, getHeight() / 2 - radius,

                        radius * 2, radius * 2);

      }

}

/*OUTPUT*/



Related Solutions

Using python Write a program that displays all of states in the U.S. and display each...
Using python Write a program that displays all of states in the U.S. and display each state that begins with the letter A.
Write an assembly program that displays the following two options: 1. Display the current date. 2....
Write an assembly program that displays the following two options: 1. Display the current date. 2. Enter a date for a meeting and display remaining days. If the user selects the first option, the program should display the current date in dd/mm/yyyy format. If the user selects the second option, the program asks the user to enter a date for a meeting within year 2020 (in decimal) in dd/mm format. The program then calculates and displays the number of days...
Part 1 Write a program that displays a frame containing two labels that display your name,...
Part 1 Write a program that displays a frame containing two labels that display your name, one for your first name and one for your last. Experiment with the size of the window to see the labels change their orientation to each other. USE FIRST NAME: Aya LAST NAME: KAYED. SEND THE CODE IN THE EXACT FORMAT FOR JAVA ECLIPSE. I WILL COPY AND PASTE THE CODE SO I CAN RUN IT. Part 2 Propose and solve your own digital...
/* Writing a program that displays the following: calculating area of a circle, calculating area of...
/* Writing a program that displays the following: calculating area of a circle, calculating area of a rectangle, calculating area of a triangle, and quit. */ import java.text.DecimalFormat; import java.util.Scanner; public class GeoCalculator {    public static void main(String arg[]) { char selection; Scanner get = new Scanner(System.in); //having user input DecimalFormat twoDigits = new DecimalFormat("0.00"); //formatting area to two decimal places //display choice of selection System.out.println("Geometry Calculator"); System.out.println("Please select from the following menu:"); System.out.println("1. Calculate the Area of a...
Write a program that displays a single character at 100 random screen locations, using a timing...
Write a program that displays a single character at 100 random screen locations, using a timing delay of 100 milliseconds. Hint: Use the GetMaxXY procedure to determine the current size of the console window. How to code this program with a defined character 'A' (I do not want to write a character to get a single character in the output console)
Please use Phyton to write a program: Write a program that calculates and displays the total...
Please use Phyton to write a program: Write a program that calculates and displays the total bill at a restaurant for a couple that is dining. The program should collect from the couple, cost of each meal, and the percentage of the final cost that they would like to tip. The sales tax in the state where the restaurant exists is 7.5%. Display to the user, line by line: Total Cost of Both Meals Sales Tax in dollars Tip in...
Write a JavaFX application that draws a circle using a rubberbanding technique. The circle size is...
Write a JavaFX application that draws a circle using a rubberbanding technique. The circle size is determined by a mouse drag. Use the initial mouse press location as the fixed center point of the circle. Compute the distance between the current location of the mouse pointer and the center point to determine the current radius of the circle.
Write a C++ program to display toy name
Write a C++ program to display toy name
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask...
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask the user if they want to roll again. If they don't want to roll again, the program should end. If they do want to roll again, the new number should display. At a minimum, the die should have 6 sides, but if you want to use a d12 or d20, that is fine too. USE PYTHON
Write a program that displays a weekly payroll report. A loop in the program should ask...
Write a program that displays a weekly payroll report. A loop in the program should ask the user for the employee number, gross pay, state tax, federal tax, and FICA withholdings. The loop will terminate when 0 is entered for the employee number. After the data is entered, the program should display totals for gross pay, state tax, federal tax, FICA withholdings, and net pay. Input Validation: Do not accept negative numbers for any of the items entered. Do not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT