Hi everyone, I am working on a problem as follows:

Write a program that draws a circle with radius 100 and center (200, 200). Ask the user to specify the x- and y-coordinates of a point. Draw the point as a small circle. If the point lies inside the circle, color the small circle green. Otherwise, color it red. In your exercise, declare a class Circle and a method boolean isInside(Point2D.Double p).


Use the following class as your main class:

import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
   This program views a circle and a pocint.
*/
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);

      Point2D.Double point = new Point2D.Double(150, 150);
            
      CircleComponent component = new CircleComponent(point);
      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.Point2D;
import java.awt.geom.Ellipse2D;

/**
   This class implements a circle and a boolean function to
   test if a user-given point is inside this circle.
*/
public class Circle
{
   private double xCenter;
   private double yCenter;
   private double radius;
   private Color color;

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

   /**
      Draws a circle and a point.
      @param g2 the graphics content
   */
   public void draw(Graphics2D g2)
   {
      g2.setColor(color);
      Ellipse2D.Double circle
         = new Ellipse2D.Double(xCenter - radius, yCenter - radius,
            2 * radius, 2 * radius);
      g2.draw(circle);
   }

   /**
      Determine if point is inside or outside the circle
      @param p the point to test if it is inside the circle
      @return true if the point is inside the circle
   */
   public boolean isInside(Point2D.Double p)
   {
      . . .
   }
}

--------------------------------------------------------------------------------

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

/**
   Draws a circle and a point. The point is colored green if it falls
   inside the circle, red otherwise.
*/
public class CircleComponent extends JComponent
{
   private Circle circle;
   private Circle smallCircle;

   public CircleComponent(Point2D.Double point)
   {
      circle = new Circle(200, 200, 100, Color.BLACK);
      final double SMALL_RADIUS = 3;
      Color color;
      if(. . .)
         color = Color.GREEN;
      else
         color = Color.RED;
      smallCircle = new Circle(point.getX(), point.getY(), SMALL_RADIUS, color);
   }

   public void paintComponent(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;
      circle.draw(g2);
      smallCircle.draw(g2);
   }
}

Use the following class in your solution:

import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
   This program views a circle and a point.
*/
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("x:");
      double x = Double.parseDouble(input);

      input = JOptionPane.showInputDialog("y:");
      double y = Double.parseDouble(input);

      Point2D.Double point = new Point2D.Double(x, y);
            
      CircleComponent component = new CircleComponent(point);
      frame.add(component);

      frame.setVisible(true);
   }
}

That is all the information we have. As I see it, there are only two places to add code, the isInside method of the Circle class and the if(...) statement of the CircleComponent method of the CircleComponent class. Below is what I have done to these. Any direction would be greatly appreciated!

public boolean isInside(Point2D.Double p)
   {
    
    boolean inside = false;
    if(xCenter * xCenter + yCenter * yCenter < radius * radius)
    {
        inside = true;
    }
    else
    {
        inside = false;
    }
    return inside;
    
   }
public CircleComponent(Point2D.Double point)
   {
      circle = new Circle(200, 200, 100, Color.BLACK);
      final double SMALL_RADIUS = 3;
      Color color;
      
      if(Circle.isInside() == true)
         color = Color.GREEN;
      else
         color = Color.RED;
      smallCircle = new Circle(point.getX(), point.getY(), SMALL_RADIUS, color);
   }

Recommended Answers

All 8 Replies

Back to School (Geometry):

What you need to do is the find the distance between the Point2D.Double p and the center of the circle; if this distance is less than the radius then its inside the circle else its outside it.

solution:
Let c represent the center of the circle
and p represent the point in question
and r represent the radius of the circle
and d be the distance between point c and point p
and _/ represents square root
^ represents power

d = _/ (c.x - p.x)^2 + (c.y - p.y)^2

if d < r then inside circle is true else inside circle is false

if(Circle.isInside() == true)

is redundant use this instead

if(Circle.isInside())

and you don't need to implement the code that calcuates distance the Point2D object already has several distance calculating methods; use them instead.

if(Circle.isInside() == true)

is redundant use this instead

if(Circle.isInside())

I get a compile error about not be able to pass static methods or argument differs in length when I try putting what you and I did in this if statement. I keep trying different combinations with no luck.

in your previous code

public CircleComponent(Point2D.Double point)
   {
      circle = new Circle(200, 200, 100, Color.BLACK);
      final double SMALL_RADIUS = 3;
      Color color;
 
      if(Circle.isInside() == true)
         color = Color.GREEN;
      else
         color = Color.RED;
      smallCircle = new Circle(point.getX(), point.getY(), SMALL_RADIUS, color);
   }

@ line 7
Java is case sensitive that would be circle.isInside(/*argument*/) since circle is your new Circle object
plus yoou need an argument that has the same argument type for public boolean isInside(Point2D.Double p)

use lower case c as in circle not Circle

Ok, great! I have it all done except the math part in the boolean isInside method of the Circle class. Here is what I have but it doesn't work, says cannot find symbol Method Pow(double, int). I cannot import anything else, the only code we add goes in this method. Any ideas?

public boolean isInside(Point2D.Double p)
   {
    
    boolean inside = false;
    //Need calculation for center of circle here
   double D = Math.Sqrt(Math.Pow(xCenter - p.x, 2) + Math.Pow(yCenter - p.y, 2)); 
    
    if (D < radius)
    {
        inside = true;
    }
    else
    {
        inside = false;
    }
    
    return inside;
    
   }

As I can see you might need to import java.lang.Math to make Math.pow and Math.sqrt to work

Got it figured out!

public boolean isInside(Point2D.Double p)
   {
    double distance;
    
    distance = Math.sqrt(Math.pow(xCenter - p.x,2)+(Math.pow(yCenter - p.y,2)));
    
    if(distance < radius)
    {
        return true;
    }
    else
    {
        return false;
    }
   }
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.