This is what I have to do: Write an application that asks the user to input the radius of a circle as a floating-point number and draws the circle, as well as the values of the circle's diameter, circumference and area. Use the value 3.14159 for pi. [Note: You may also use the predefined constant Math.PI for the value of pi. This constant is more precise than the value 3.14159. Class Math is declared in the java.lang package, so you need not import it.] Use the following formulas (r is the radius):
diameter = 2r
circumference = 2PIr
area = PI r^2

The user should also be prompted for a set of coordinates in ddition to the radius. Then draw the circle and display its diameter, circumference, and area, using an Ellipse2D.Double object to represent the circle and mehtod draw of class Graphics2D to display it.


import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

class Circle extends JComponent{
   private double radius;

public Circle(double r)
    {radius = r;}
public void drawCircle(Graphics gr){
    Graphics2D g = (Graphics2D)gr;
    Ellipse2D.Double ellipse = new Ellipse2D.Double(0,0,2.0*radius,2.0*radius);
  public static void main(String[] args) {

   double radius=Double.parseDouble(JOptionPane.showInputDialog( "Enter radius of the circle" ));
   double diameter;
   double circumference;
   double area;
   diameter = 2 * radius;
   circumference = 2 * Math.PI * radius;
   area = Math.PI * radius * radius;
   JLabel diameter2 = new JLabel("Diameter: " + diameter);
   JLabel circumference2 = new JLabel("Circumference: " + circumference);
   JLabel area2 = new JLabel("Area: " + area);

JPanel text = new JPanel(new GridLayout(2,2));
JFrame frame = new JFrame();
Circle c = new Circle(radius);
frame.setTitle("Draw a Circle");

It runs, but it doesn't draw the circle. How come?

Also, I want to know where I would put the part that asks for the coordinates? Like would I put it after they enter the radius and how would I set up the coordinate part?

This article has been dead for over six months. Start a new discussion instead.