import java.awt.*;
import java.awt.geom.*;

/**
   A circular node that is filled with a color.
*/
public class CircleNode implements Node
{
   /**
      Construct a circle node with a given size and color.
      @param size the size
      @param aColor the fill color
   */
   public CircleNode(Color aColor)
   {
      size = DEFAULT_SIZE;
      x = 0;
      y = 0;
      color = aColor;
   }

   public Object clone()
   {
      try
      {
         return super.clone();
      }
      catch (CloneNotSupportedException exception)
      {
         return null;
      }
   }

   public void draw(Graphics2D g2)
   {
      Ellipse2D circle = new Ellipse2D.Double(
         x, y, size, size);
      Color oldColor = g2.getColor();
      g2.setColor(color);
      g2.fill(circle);
      g2.setColor(oldColor);
      g2.draw(circle);
   }

   public void translate(double dx, double dy)
   {
      x += dx;
      y += dy;
   }

   public boolean contains(Point2D p)
   {
      Ellipse2D circle = new Ellipse2D.Double(
         x, y, size, size);
      return circle.contains(p);
   }

   public Rectangle2D getBounds()
   {
      return new Rectangle2D.Double(
         x, y, size, size);
   }

   public Point2D getConnectionPoint(Point2D other)
   {
      double centerX = x + size / 2;
      double centerY = y + size / 2;
      double dx = other.getX() - centerX;
      double dy = other.getY() - centerY;
      double distance = Math.sqrt(dx * dx + dy * dy);
      if (distance == 0) return other;
      else return new Point2D.Double(
         centerX + dx * (size / 2) / distance,
         centerY + dy * (size / 2) / distance);
   }

   private int x;
   private int y;
   private int size;
   private Color color;  
   private static final int DEFAULT_SIZE = 20;
}
