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

Write a Java program named CircleZapper that displays a circle with a radius of 10 pixels,...
Write a Java program named CircleZapper that displays a circle with a radius of 10 pixels, filled with a random color at a random location on the screen. When you click the circle, it disappears and a new random color circle is displayed at another random location (see display below). After twenty circles are clicked, display the time spent in the pane. To detect whether a point is inside the circle, use the contains method defined in the Node class....
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 a program that displays a frame containing two labels that display your name, one for...
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.
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...
Write a C++ program that makes a 10x10 array and picks random letters and displays in...
Write a C++ program that makes a 10x10 array and picks random letters and displays in different positions
/* 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)
Display the shortest words in a sentence in JAVA Write a method that displays all the...
Display the shortest words in a sentence in JAVA Write a method that displays all the shortest words in a given sentence. You are not allowed to use array In the main method, ask the user to enter a sentence and call the method above to display all the shortest words in a given sentence. You must design the algorithms for both the programmer-defined method and the main method.
Write a C program that creates a structure and displays its content. • Create a struct...
Write a C program that creates a structure and displays its content. • Create a struct that will be used to hold a student's name, age, and year in school (Freshman, Sophomore, Junior, or Senior) • Within function main, dynamically allocate space to hold the structure and assign a pointer to point to the memory space allocated • Read in (from the keyboard) the student's name, age, and year in school • Create a separate function with the prototype: void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT