1 Replies - 1615 Views - Last Post: 21 November 2011 - 04:52 PM Rate Topic: -----

#1 TuckNorris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-November 11

Intersecting Circles

Posted 21 November 2011 - 04:29 PM

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 color both circles green. Otherwise, color them red. 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. In your exercise, declare a class Circle and a method boolean intersects(Circle other).

Here is a sample program output:

Use the following class as your main class:
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
   This program views two circles.
*/
public class CircleViewerSnap
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();

      final int FRAME_WIDTH = 400;
      final int FRAME_HEIGHT = 400;

      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setTitle("CircleViewer");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      CircleComponent component = new CircleComponent(60, 85);
      frame.add(component);

      frame.setVisible(true);
   }
}

Complete the following classes in your solution:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

/**
   This class implements a Circle. It includes a method to test whether 
   two circles intersect.
*/
public class Circle
{
   private double xCenter;
   private double yCenter;
   private double radius;
   private Color color;

   /**
      Constructs a black circle.
      @param x the x-coordinate of the center
      @param y the y-coordinate of the center
      @param r the radius
   */
   public Circle(double x, double y, double r)
   {
      xCenter = x;
      yCenter = y;
      radius = r;
      color = Color.BLACK;
   }
   
   /**
      Sets the color of this circle.
      @param aColor the color
   */
   public void setColor(Color aColor)
   {
      color = aColor;
   }

   /**
      Draws the circles.
      @param g2 the graphics context
   */
   public void draw(Graphics2D g2)
   {
      if (radius < 0)
         return;

      g2.setColor(color);
      // draw the circle
      Ellipse2D.Double circle
         = new Ellipse2D.Double(xCenter - radius, yCenter - radius,
            2 * radius, 2 * radius);
      g2.draw(circle);
   }

   /**
      Tests whether or not the two circles intersect with each other.
      @param other the other circle
      @return true the two circles intersect
   */
   public boolean intersects(Circle other)
   {
      . . .
   }
}

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import javax.swing.JComponent;
import javax.swing.JPanel;

/**
   Shows two Circles and tests whether they intersect or not.
 */
public class CircleComponent extends JComponent
{
   private Circle circle1;
   private Circle circle2;

 
   /**
      Constructs a component for showing two circles.
      @param r1 the radius of the first circle
      @param r2 the radius of the second circle
   */
   public CircleComponent(double r1, double r2)
   {
      String message;

      circle1 = new Circle(100, 200, r1);
      circle2 = new Circle(200, 100, r2);
      Color color;
      if (. . .)
         color = Color.GREEN;
      else
         color = Color.RED;
      circle1.setColor(color);
      circle2.setColor(color);
   }

   public void paintComponent(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;

      circle1.draw(g2);
      circle2.draw(g2);
   }
}

Use the following class in your solution:

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
   This program views two circles.
*/
public class CircleViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();

      final int FRAME_WIDTH = 400;
      final int FRAME_HEIGHT = 400;

      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setTitle("CircleViewer");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      String input = JOptionPane.showInputDialog("Radius 1:");
      double r1 = Double.parseDouble(input);

      input = JOptionPane.showInputDialog("Radius 2:");
      double r2 = Double.parseDouble(input);

      CircleComponent component = new CircleComponent(r1, r2);
      frame.add(component);

      frame.setVisible(true);
   }
}




what to put inplace of . . . ?

Is This A Good Question/Topic? 0
  • +

Replies To: Intersecting Circles

#2 TuckNorris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-November 11

Re: Intersecting Circles

Posted 21 November 2011 - 04:52 PM

I've tried making a 2D Line from centerx to centery.
Then finding the midpoint and comparing it to the radii.

Shouldn't this work?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1