Source of AbstractEdge.java


  1: import java.awt.BasicStroke;
  2: import java.awt.Graphics2D;
  3: import java.awt.Shape;
  4: import java.awt.geom.GeneralPath;
  5: import java.awt.geom.Line2D;
  6: import java.awt.geom.Point2D;
  7: import java.awt.geom.Rectangle2D;

  9: /**
 10:    A class that supplies convenience implementations for 
 11:    a number of methods in the Edge interface type.
 12: */
 13: public abstract class AbstractEdge implements Edge
 14: {  
 15:    public Object clone()
 16:    {
 17:       try
 18:       {
 19:          return super.clone();
 20:       }
 21:       catch (CloneNotSupportedException exception)
 22:       {
 23:          return null;
 24:       }
 25:    }

 27:    public void connect(Node s, Node e)
 28:    {  
 29:       start = s;
 30:       end = e;
 31:    }

 33:    public Node getStart()
 34:    {
 35:       return start;
 36:    }

 38:    public Node getEnd()
 39:    {
 40:       return end;
 41:    }

 43:    public Rectangle2D getBounds(Graphics2D g2)
 44:    {
 45:       Line2D conn = getConnectionPoints();      
 46:       Rectangle2D r = new Rectangle2D.Double();
 47:       r.setFrameFromDiagonal(conn.getX1(), conn.getY1(),
 48:          conn.getX2(), conn.getY2());
 49:       return r;
 50:    }

 52:    public Line2D getConnectionPoints()
 53:    {
 54:       Rectangle2D startBounds = start.getBounds();
 55:       Rectangle2D endBounds = end.getBounds();
 56:       Point2D startCenter = new Point2D.Double(
 57:          startBounds.getCenterX(), startBounds.getCenterY());
 58:       Point2D endCenter = new Point2D.Double(
 59:          endBounds.getCenterX(), endBounds.getCenterY());
 60:       return new Line2D.Double(
 61:          start.getConnectionPoint(endCenter),
 62:          end.getConnectionPoint(startCenter));
 63:    }

 65:    private Node start;
 66:    private Node end;
 67: }