001package horstmann.ch08_umleditor; 002import java.awt.BasicStroke; 003import java.awt.Graphics2D; 004import java.awt.Shape; 005import java.awt.geom.Line2D; 006import java.awt.geom.Point2D; 007import java.awt.geom.Rectangle2D; 008 009/** 010 A class that assumes that an edge can yield its shape 011 and then takes advantage of the fact that containment testing can 012 be done by stroking the shape with a fat stroke. 013 */ 014@SuppressWarnings("serial") 015public abstract class ShapeEdge extends AbstractEdge 016{ 017 /** 018 Returns the path that should be stroked to 019 draw this edge. The path does not include 020 arrow tips or labels. 021 @return a path along the edge 022 */ 023 public abstract Shape getShape(); 024 025 public Rectangle2D getBounds(Graphics2D g2) 026 { 027 return getShape().getBounds(); 028 } 029 030 public boolean contains(Point2D aPoint) 031 { 032 final double MAX_DIST = 3; 033 034 // the end points may contain small nodes, so don't 035 // match them 036 Line2D conn = getConnectionPoints(); 037 if (aPoint.distance(conn.getP1()) <= MAX_DIST 038 || aPoint.distance(conn.getP2()) <= MAX_DIST) 039 return false; 040 041 Shape p = getShape(); 042 BasicStroke fatStroke = new BasicStroke( 043 (float)(2 * MAX_DIST)); 044 Shape fatPath = fatStroke.createStrokedShape(p); 045 return fatPath.contains(aPoint); 046 } 047}