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.

code:

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);
    g.draw(ellipse);
}
  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");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(c,BorderLayout.CENTER);
text.add(diameter2);
text.add(circumference2);
text.add(area2);
frame.add(text,BorderLayout.SOUTH);
frame.setSize(600,400);
frame.setVisible(true);
}
}

Issues:
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?

Dani AI

Generated

Your class never gets asked to paint itself. Swing calls paintComponent, not your custom drawCircle method, so the shape never appears. As hinted, follow Swing painting rules: override protected void paintComponent(Graphics), call super.paintComponent(g) first, then draw with Graphics2D. Move your drawing logic into that method (or call your drawCircle from there). Prompting for coordinates should happen before you construct and add the component, then pass them into the component so paintComponent can use them.

A minimal example of the painting method (placed inside your Circle JComponent) — note the conversion from center coordinates to top-left for Ellipse2D.Double:

@Override
protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D) g;
  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  double topLeftX = centerX - radius;
  double topLeftY = centerY - radius;
  Ellipse2D.Double circle = new Ellipse2D.Double(topLeftX, topLeftY, radius * 2, radius * 2);
  g2.draw(circle);
}

Practical tips: ask for centerX/centerY (after radius) and treat them as pixels; if you want center coordinates relative to the component, convert accordingly. Use setPreferredSize on the component and call frame.pack() (or ensure frame size is large enough) so the circle is not clipped. Create GUI on the EDT (SwingUtilities.invokeLater) and call repaint() after changing values.

Drawing shapes in Java has to be done in a very specific way - here's a tutorial that will show you exactly what to do:
http://docs.oracle.com/javase/tutorial/uiswing/painting/

You can handle the coordinates in exactly the same way you did the radius.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.