001package horstmann.ch08_graphed;
002import java.awt.Graphics2D;
003import java.awt.geom.Line2D;
004import java.awt.geom.Point2D;
005import java.awt.geom.Rectangle2D;
006
007/**
008   A class that supplies convenience implementations for
009   a number of methods in the Edge interface type.
010 */
011@SuppressWarnings("serial")
012public abstract class AbstractEdge implements Edge
013{
014        public Object clone()
015        {
016                try
017                {
018                        return super.clone();
019                }
020                catch (CloneNotSupportedException exception)
021                {
022                        return null;
023                }
024        }
025
026        public void connect(Node s, Node e)
027        {
028                start = s;
029                end = e;
030        }
031
032        public Node getStart()
033        {
034                return start;
035        }
036
037        public Node getEnd()
038        {
039                return end;
040        }
041
042        public Rectangle2D getBounds(Graphics2D g2)
043        {
044                Line2D conn = getConnectionPoints();
045                Rectangle2D r = new Rectangle2D.Double();
046                r.setFrameFromDiagonal(conn.getX1(), conn.getY1(),
047                                conn.getX2(), conn.getY2());
048                return r;
049        }
050
051        public Line2D getConnectionPoints()
052        {
053                Rectangle2D startBounds = start.getBounds();
054                Rectangle2D endBounds = end.getBounds();
055                Point2D startCenter = new Point2D.Double(
056                                startBounds.getCenterX(), startBounds.getCenterY());
057                Point2D endCenter = new Point2D.Double(
058                                endBounds.getCenterX(), endBounds.getCenterY());
059                return new Line2D.Double(
060                                start.getConnectionPoint(endCenter),
061                                end.getConnectionPoint(startCenter));
062        }
063
064        private Node start;
065        private Node end;
066}