DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2002

    Exclamation 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");
    init_radius1 = Integer.parseInt(input1);
    String input2 = JOptionPane.showInputDialog("Enter the Radius of the second circle");
    init_radius2 = Integer.parseInt(input2);

    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
    double radius1 = init_radius1;
    double radius2 = init_radius2;

    //Draws the first circle
    Ellipse2D.Double circle1
    = new Ellipse2D.Double(x1Center - radius1, y1Center - radius1, 2 * radius1, 2 * radius1);

    //Draws the second circle
    Ellipse2D.Double circle2
    = new Ellipse2D.Double(x2Center - radius2, y2Center - radius2, 2 * radius2, 2 * radius2);


    private final double init_radius1;
    private final double init_radius2;

    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.

    Please help. I've been on my *** tryin to do this for hours now...

  2. #2
    Join Date
    Sep 2002
    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

    //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. #3
    Join Date
    Aug 2002
    you may consider following for adding to your code:

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.