Skip to content

22 7_RandomCircle_Easy

Wil Simpson edited this page Jul 30, 2021 · 6 revisions

Portfolio Home Page

Problem Statement

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 x radius, area = π x radius^2, circumference = 2 x π x radius. Use the constant Math.PI for pi (π). All drwaing should be done on a subclass of JPanel, and the results of the calculations should be displayed in a read-only JTextArea.

User Documentation

To run the program and generate a random circle with it's properties displayed run the main class RandomCircleDriver. Once compiled and ran the program will display a random circle and it's properties. To generate a new circle do File -> New Circle or the keyboard shortcut Ctrl + N.

Developer Documentation

The program starts in the RandomCircleDriver class and creates a new instance of RandomCircle which extends JFrame and uses the CirclePanel class which extends JPanel to handle drawing of the circles. RandomCircle sets up the frame, layout, menu, action listeners and creates a new instance of MyCircle. To show the frame and finish initialization the run() method must be called inside the class. CirclePanel handles the drawing via calls to paintComponent(Graphics) and creation of the MyCircle instances through initialization or calls to generateNewCircle(). MyCircle constructor creates a new circle with a minimum and maximum radius between MIN_RADIUS and MAX_RADIUS defined in the class. The methods such as getRadius(), getDiameter(), getArea()andgetCircumference()` calculate these properties for the given radius.

UML Diagram: UML Diagram

JavaDocs

The java documents are served from a local web server on this machine. To start the web server, navigate to the directory immediately above where the source code is checked out (i.e. ~/git ) and then use "python -m SimpleHTTPServer" in that directory.

cd ~/git
python -m SimpleHTTPServer&

Note: if you are running python 3 (which you can check via opening a terminal and typing: python --version), then the command is:

python3 -m http.server

Click Here to View JavaDocs

Source Code

Link to the source code