Connectors are algorithms (Strategy design pattern) that are used by ConnectionFigures to decide where the point of attachment for the connection should be placed. JHotDraw comes pre supplied with several different types of connector. The Connector interface is implemented by AbstractConnector which in turn is extended by ChopBoxConnector, LocatorConnector and ShortestDistanceConnector.
To illustrate the role a connector plays consider an EllipseFigure on a drawing connected to another figure. To make the connection appear to link the two figures together the line must be drawn from the perimeter of one figure to the perimeter of the other. This implies that a ConnectionFigure knows where the edges of both figures lie. However JHotDraw considers figures to be rectangular areas containing a visual representation of the shape. This precludes the connection from identifying the edge of the shape inside the figure (it can only see the figure's rectangle). To overcome this problem JHotDraw introduces the concept of Connectors. The ChopBoxConnector and its subclasses ChopEllipseConnector, ChopPolygonConnector and PolyLineConnector can be used to define the points that lie on the perimeter of the figure. ConnectionFigures attached to the Figure can query its connector to determine the figures actual perimeter.
A connection between ellipses showing the use of connectors
Connectors belong to Figures and can be obtained by calling a figures connectorAt(int,int) method. This method will return the connector that covers the point defined by the two integers in the parameter list. Figures can contain several different types of connector (covering different areas of the figure) but typically they contain only one.
Figures can be prevented form taking part in connections by overriding the canConnect() method and making it return false.
Connectors can be defined visually by using the ConnectionHandle class to create a Handle for connections.