    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 void setColor(Color aColor)
    22:    {
    23:       color = aColor;
    24:    }
    25:
    26:    public Color getColor()
    27:    {
    28:       return color;
    29:    }
    30:
    31:    public Object clone()
    32:    {
    33:       try
    34:       {
    35:          return super.clone();
    36:       }
    37:       catch (CloneNotSupportedException exception)
    38:       {
    39:          return null;
    40:       }
    41:    }
    42:
    43:    public void draw(Graphics2D g2)
    44:    {
    45:       Ellipse2D circle = new Ellipse2D.Double(
    46:          x, y, size, size);
    47:       Color oldColor = g2.getColor();
    48:       g2.setColor(color);
    49:       g2.fill(circle);
    50:       g2.setColor(oldColor);
    51:       g2.draw(circle);
    52:    }
    53:
    54:    public void translate(double dx, double dy)
    55:    {
    56:       x += dx;
    57:       y += dy;
    58:    }
    59:
    60:    public boolean contains(Point2D p)
    61:    {
    62:       Ellipse2D circle = new Ellipse2D.Double(
    63:          x, y, size, size);
    64:       return circle.contains(p);
    65:    }
    66:
    67:    public Rectangle2D getBounds()
    68:    {
    69:       return new Rectangle2D.Double(
    70:          x, y, size, size);
    71:    }
    72:
    73:    public Point2D getConnectionPoint(Point2D other)
    74:    {
    75:       double centerX = x + size / 2;
    76:       double centerY = y + size / 2;
    77:       double dx = other.getX() - centerX;
    78:       double dy = other.getY() - centerY;
    79:       double distance = Math.sqrt(dx * dx + dy * dy);
    80:       if (distance == 0) return other;
    81:       else return new Point2D.Double(
    82:          centerX + dx * (size / 2) / distance,
    83:          centerY + dy * (size / 2) / distance);
    84:    }
    85:
    86:    private int x;
    87:    private int y;
    88:    private int size;
    89:    private Color color;
    90:    private static final int DEFAULT_SIZE = 20;
    91: }