DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Sep 2002
Posts
1

## Need HELP on THIS PLEASE!!!!!!

I'm stuck and I hate this. Someone help me out please.
Program Assignment:

Write a graphics program that asks the user to specify the radii of two circles. The first circle has center (100,200), and the second circle has center(200,100). Draw the circles. If they intersect, then display a message "Circles intersect." Otherwise, display "Circles don't intersect." Hint: Compute the distance between the centers and compare it to the radii. Your program should draw nothing if the user enters a negative radius.

I need a Circle.java file and a CircleIntersectApplet.java file.

Here is what I have so far:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JOptionPane;

public class CircleIntersectApplet extends Applet
{
public CircleIntersectApplet()
{
String input1 = JOptionPane.showInputDialog("Enter the Radius of the first circle");
String input2 = JOptionPane.showInputDialog("Enter the Radius of the second circle");
}

public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
final double x1Center = 100; //Fixed Center Point
final double y1Center = 200; //Fixed Center Point
final double x2Center = 200; //Fixed Center Point
final double y2Center = 100; //Fixed Center Point

//Draws the first circle
Ellipse2D.Double circle1
g2.draw(circle1);

//Draws the second circle
Ellipse2D.Double circle2
g2.draw(circle2);

}

}

When this is ran in an Appletviewer, it asks the User to input the First radius, then the Second Radius. After it is inputed, it draws two circles with the inputed Radius'.

Now, I still need the if statements to compute the distance between the two centers = d, and the two Radius = r1 and r2.

something like: if d > r1 + r2 then they don't intersect.

I don't know how to write If else statements and don't know where I should plug it in.

Also, I have to submit in 2 files... not 1. So I don't know what should be in the first file and the second one.

Lastly, if they do intersect, it needs to say "Circles intersect." and if they don't intersect, it needs to say "Circles don't intersect." in the applet with the 2 circles showing.

2. Banned
Join Date
Sep 2002
Posts
124
Hi, Skunk2nR

To get things started, the IF / ELSE statements in Java aren't too difficult. The code contained within the two braces {} will only execute IF the condition within the parentheses () is met.

if (d > r1 + r2)
{
// code here will alert the user
// to the fact that circles intersect
}

else
{
//code here to deal with the
// circles NOT intersecting
}

You'll need another IF statement to deal with the drawing of the circles and NEGATIVE numbers. If the numbers ARE NOT negative the cirlces are drawn.

I'm not totally sure why you need to have two classes here. Would the Circle.java deal with the code to draw the circles...and would the applet code use this to draw the circles once the user has input the desired numbers?

Hope this is of some use to you
Joyous Monkey

3. Registered User
Join Date
Aug 2002
Posts
94
Hi,

1) Add a method that takes two points as input and returns the distance between two points :

public double distance(int x, int y){
//this step adds the square of x & y and then compute the square root i.e. distance between them.

double dist=java.lang.Math.pow(x*x+y*y, 0.5);

return (dist);
}

2) Since you are using Graphics2D then you can draw circle like this:

g.drawOval(int x,int y,int width,int height);
/*
x - the x coordinate of the upper left corner of the oval to be drawn.
y - the y coordinate of the upper left corner of the oval to be drawn.
width - the width of the oval to be drawn.
height - the height of the oval to be drawn.
Thus if width=height then it will draw a circle.
*/
3) You can eliminate few lines by just adding import java.awt.* instead of importing each class individually - unless you are trying to learn the fundamentals of java packages.
bye
Pankaj

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise