    01: import java.awt.*;
    02: import java.awt.geom.*;
    03:
    04: /**
    05:    A circular node that is filled with a color.
    06: */
    07: public class CircleNode implements Node
    08: {
    09:    /**
    10:       Construct a circle node with a given size and color.
    11:       @param aColor the fill color
    12:    */
    13:    public CircleNode(Color aColor)
    14:    {
    15:       size = DEFAULT_SIZE;
    16:       x = 0;
    17:       y = 0;
    18:       color = aColor;
    19:    }
    20:
    21:    public Object clone()
    22:    {
    23:       try
    24:       {
    25:          return super.clone();
    26:       }
    27:       catch (CloneNotSupportedException exception)
    28:       {
    29:          return null;
    30:       }
    31:    }
    32:
    33:    public void draw(Graphics2D g2)
    34:    {
    35:       Ellipse2D circle = new Ellipse2D.Double(
    36:          x, y, size, size);
    37:       Color oldColor = g2.getColor();
    38:       g2.setColor(color);
    39:       g2.fill(circle);
    40:       g2.setColor(oldColor);
    41:       g2.draw(circle);
    42:    }
    43:
    44:    public void translate(double dx, double dy)
    45:    {
    46:       x += dx;
    47:       y += dy;
    48:    }
    49:
    50:    public boolean contains(Point2D p)
    51:    {
    52:       Ellipse2D circle = new Ellipse2D.Double(
    53:          x, y, size, size);
    54:       return circle.contains(p);
    55:    }
    56:
    57:    public Rectangle2D getBounds()
    58:    {
    59:       return new Rectangle2D.Double(
    60:          x, y, size, size);
    61:    }
    62:
    63:    public Point2D getConnectionPoint(Point2D other)
    64:    {
    65:       double centerX = x + size / 2;
    66:       double centerY = y + size / 2;
    67:       double dx = other.getX() - centerX;
    68:       double dy = other.getY() - centerY;
    69:       double distance = Math.sqrt(dx * dx + dy * dy);
    70:       if (distance == 0) return other;
    71:       else return new Point2D.Double(
    72:          centerX + dx * (size / 2) / distance,
    73:          centerY + dy * (size / 2) / distance);
    74:    }
    75:
    76:    private int x;
    77:    private int y;
    78:    private int size;
    79:    private Color color;
    80:    private static final int DEFAULT_SIZE = 20;
    81: }