Source of ShapeEdge.java


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

  8: /**
  9:    A class that assumes that an edge can yield its shape
 10:    and then takes advantage of the fact that containment testing can 
 11:    be done by stroking the shape with a fat stroke.
 12: */
 13: public abstract class ShapeEdge extends AbstractEdge
 14: {  
 15:    /**
 16:       Returns the path that should be stroked to
 17:       draw this edge. The path does not include
 18:       arrow tips or labels.
 19:       @return a path along the edge
 20:    */
 21:    public abstract Shape getShape();

 23:    public Rectangle2D getBounds(Graphics2D g2)
 24:    {
 25:       return getShape().getBounds();
 26:    }

 28:    public boolean contains(Point2D aPoint)
 29:    {
 30:       final double MAX_DIST = 3;

 32:       // the end points may contain small nodes, so don't
 33:       // match them
 34:       Line2D conn = getConnectionPoints();
 35:       if (aPoint.distance(conn.getP1()) <= MAX_DIST 
 36:          || aPoint.distance(conn.getP2()) <= MAX_DIST)
 37:          return false;

 39:       Shape p = getShape();
 40:       BasicStroke fatStroke = new BasicStroke(
 41:          (float)(2 * MAX_DIST));
 42:       Shape fatPath = fatStroke.createStrokedShape(p);
 43:       return fatPath.contains(aPoint);
 44:    }
 45: }