Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: making a program that draws two circles and calculates the area and circumference

1. ## making a program that draws two circles and calculates the area and circumference

I have a program that I need to finish before tomorrow and here are the instructions:

To draw two circles plus a line connecting their centers and then print a few facts about the circles. The (x, y) coordinates and radii of the circles must be accepted from the user.
The applet’s algorithm is as follows:
1. Ask the user for the x-coordinate for the location of a circle.
2. Ask the user for the y-coordinate for the location of a circle.
3. Ask the user for the x-coordinate for the location of another circle.
4. Ask the user for the y-coordinate for the location of another circle.
7. Draw both circles using the x, y coordinates and radii given by the user.
8. Draw a line connecting both centers.
9. Write the following facts about the circles:
a. Each circle’s area
b. Each circle’s circumference
c. Whether or not the circles touch each other.

Requirements:
•This program must extend a JFrame.
•Set the size of the program to 500x500 using the setSize(500, 500); function.
•Remember that when you draw the circles, you must use the drawOval() function, but it will NOT use the center of the circle as the x,y coordinate, it will use the upper-left corner of the shape. So you must adjust the ends of the line segment so that it connects the circles by their centers (add the radius to the x and y coordinates of the circles to do this).
•Use the drawString() function to display the messages about the circles. The numbers for the area and circumference should be formatted using %1.3f. This can be done by using the String class’s format() function for example:
drawstring(String.format(“area = %1.3f”, circle1Area));
•Your program class should contain private double variables to store the circle data.
•Use Math.PI to get the value of Pi (3.14159…) for calculating the area and circumference.
•To find out if the circles overlap, you can use the Distance Formula (look it up) to find the distance between their centers, then compare that distance to the sum of the two circles’ radii. If the distance is less than or equal to the sum of the radii, they overlap.

The problems I'm having is basically Steps 7-9 and A, B and C. I don't know how I'm supposed to do the string formatting, drawing the circles, drawing a line connecting their centers and how to calculate the area and circumference. So when I run this, it does the first 6 steps and then the window shows up blank because I don't have the rest of the information. I have included a picture of what its supposed to look like, sorry that the circles got cut off, but that's what it kind of is supposed show. You can kind of see that the line connects the centers.

Image.jpgHere is the code I have so far:

```import javax.swing.JFrame;
import java.awt.Graphics;
import javax.swing.JOptionPane;
import java.lang.Math;
import java.util.Scanner;
/**
*
* @author Nate
*/
public class Program1 extends JFrame {
private static final int FRAME_SIZE = 500;
private static final double PI =3.14159;
private static final double X1 = 100;
private static final double X2 = 100;
private static final double Y1 = 150;
private static final double Y2 = 200;
private static final double R1 = 50;
private static final double R2 = 60;

public void paintComponent(Graphics canvas)
{
super.paintComponents(canvas);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics canvas = (Graphics)g;
}

public static void main(String[] args) {

String circle1XcoordinateString =
JOptionPane.showInputDialog("Enter the X-Coordinate of circle #1");
int circle1X = Integer.parseInt(circle1XcoordinateString);

String circle2XcoordinateString =
JOptionPane.showInputDialog("Enter the X-Coordinate of circle #2");
int circle2X = Integer.parseInt(circle2XcoordinateString);

String circle1YcoordinateString =
JOptionPane.showInputDialog("Enter the Y-Coordinate of circle #1");
int circle1Y = Integer.parseInt(circle1YcoordinateString);

String circle2YcoordinateString =
JOptionPane.showInputDialog("Enter the Y-Coordinate of circle #2");
int circle2Y = Integer.parseInt(circle2YcoordinateString);

JOptionPane.showInputDialog("Enter the radius of circle #1");

JOptionPane.showInputDialog("Enter the radius of circle #2");

JFrame guiWindow = new JFrame();
guiWindow.setVisible(true);
guiWindow.setSize(500, 500);
guiWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}```  Reply With Quote

3. ## Re: making a program that draws two circles and calculates the area and circumference

The JFrame class does not have a paintComponent method that you can override, so that part of your code will not be called by the JVM to do anything. You override the paintComponent method for Swing classes.

You need to look at the API doc for the Graphics class to see what methods you can use to draw a circle. I think the one you want draws an oval with the width and height the same. Add a call to this method to your paint method and see what is drawn on the screen. Be sure to set the Graphics color to a contrasting color so your drawing will be visible.  Reply With Quote