SE450: Framework Classes [42/63] Previous pageContentsNext page

file:horstmann/ch08_graphed/Node.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package horstmann.ch08_graphed;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;

/**
   A node in a graph.
 */
public interface Node extends Serializable, Cloneable
{
  /**
      Draw the node.
      @param g2 the graphics context
   */
  void draw(Graphics2D g2);

  /**
      Translates the node by a given amount.
      @param dx the amount to translate in the x-direction
      @param dy the amount to translate in the y-direction
   */
  void translate(double dx, double dy);

  /**
      Tests whether the node contains a point.
      @param aPoint the point to test
      @return true if this node contains aPoint
   */
  boolean contains(Point2D aPoint);

  /**
      Get the best connection point to connect this node
      with another node. This should be a point on the boundary
      of the shape of this node.
      @param aPoint an exterior point that is to be joined
      with this node
      @return the recommended connection point
   */
  Point2D getConnectionPoint(Point2D aPoint);

  /**
      Get the bounding rectangle of the shape of this node
      @return the bounding rectangle
   */
  Rectangle2D getBounds();

  Object clone();
}

file:horstmann/ch08_graphed/Edge.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package horstmann.ch08_graphed;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;

/**
   An edge in a graph.
 */
public interface Edge extends Serializable, Cloneable
{
  /**
      Draw the edge.
      @param g2 the graphics context
   */
  void draw(Graphics2D g2);

  /**
      Tests whether the edge contains a point.
      @param aPoint the point to test
      @return true if this edge contains aPoint
   */
  boolean contains(Point2D aPoint);

  /**
      Connects this edge to two nodes.
      @param aStart the starting node
      @param anEnd the ending node
   */
  void connect(Node aStart, Node anEnd);

  /**
      Gets the starting node.
      @return the starting node
   */
  Node getStart();

  /**
      Gets the ending node.
      @return the ending node
   */
  Node getEnd();

  /**
      Gets the points at which this edge is connected to
      its nodes.
      @return a line joining the two connection points
   */
  Line2D getConnectionPoints();

  /**
      Gets the smallest rectangle that bounds this edge.
      The bounding rectangle contains all labels.
      @return the bounding rectangle
   */
  Rectangle2D getBounds(Graphics2D g2);

  Object clone();
}

file:horstmann/ch08_graphed/AbstractEdge.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package horstmann.ch08_graphed;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
   A class that supplies convenience implementations for
   a number of methods in the Edge interface type.
 */
@SuppressWarnings("serial")
public abstract class AbstractEdge implements Edge
{
  public Object clone()
  {
    try
    {
      return super.clone();
    }
    catch (CloneNotSupportedException exception)
    {
      return null;
    }
  }

  public void connect(Node s, Node e)
  {
    start = s;
    end = e;
  }

  public Node getStart()
  {
    return start;
  }

  public Node getEnd()
  {
    return end;
  }

  public Rectangle2D getBounds(Graphics2D g2)
  {
    Line2D conn = getConnectionPoints();
    Rectangle2D r = new Rectangle2D.Double();
    r.setFrameFromDiagonal(conn.getX1(), conn.getY1(),
        conn.getX2(), conn.getY2());
    return r;
  }

  public Line2D getConnectionPoints()
  {
    Rectangle2D startBounds = start.getBounds();
    Rectangle2D endBounds = end.getBounds();
    Point2D startCenter = new Point2D.Double(
        startBounds.getCenterX(), startBounds.getCenterY());
    Point2D endCenter = new Point2D.Double(
        endBounds.getCenterX(), endBounds.getCenterY());
    return new Line2D.Double(
        start.getConnectionPoint(endCenter),
        end.getConnectionPoint(startCenter));
  }

  private Node start;
  private Node end;
}

Previous pageContentsNext page