JHotDraw 7.3.1

org.jhotdraw.draw
Class BezierFigure

java.lang.Object
  extended by org.jhotdraw.beans.AbstractBean
      extended by org.jhotdraw.draw.AbstractFigure
          extended by org.jhotdraw.draw.AbstractAttributedFigure
              extended by org.jhotdraw.draw.BezierFigure
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Figure, DOMStorable
Direct Known Subclasses:
LineFigure, ODGBezierFigure, SVGBezierFigure

public class BezierFigure
extends AbstractAttributedFigure

A Figure which draws an opened or a closed bezier path.

A bezier figure can be used to draw arbitrary shapes using a BezierPath. It can be used to draw an open path or a closed shape.

A BezierFigure can have straight path segments and curved segments. A straight path segment can be added by clicking on the drawing area. Curved segments can be added by dragging the mouse pointer over the drawing area.

To creation of the BezierFigure can be finished by adding a segment which closes the path, or by double clicking on the drawing area, or by selecting a different tool in the DrawingEditor.


Design Patterns

Strategy
LineDecoration encapsulats a strategy for drawing line decorations of a BezierFigure.
Strategy: LineDecoration; Context: BezierFigure.


Version:
$Id: BezierFigure.java 575 2009-10-18 11:26:50Z rawcoder $
Author:
Werner Randelshofer
See Also:
BezierPath, Serialized Form

Field Summary
protected  BezierPath path
          The BezierPath.
 
Fields inherited from class org.jhotdraw.draw.AbstractFigure
changingDepth, listenerList
 
Fields inherited from class org.jhotdraw.beans.AbstractBean
propertySupport
 
Constructor Summary
BezierFigure()
          Creates an empty BezierFigure, for example without any BezierPath.Nodes.
BezierFigure(boolean isClosed)
          Creates an empty BezierFigure, for example without any BezierPath.Nodes.
 
Method Summary
 void addNode(BezierPath.Node p)
          Adds a control point.
 void addNode(int index, BezierPath.Node p)
          Adds a node to the list of points.
 boolean canConnect()
          Checks if this figure can be connected.
 java.awt.geom.Point2D.Double chop(java.awt.geom.Point2D.Double p)
           
 BezierFigure clone()
          Returns a clone of the figure, with clones of all aggregated figures, such as children and decorators.
 boolean contains(java.awt.geom.Point2D.Double p)
          Checks if a point is contained by the figure.
 java.util.Collection<Handle> createHandles(int detailLevel)
          Creates handles used to manipulate the figure.
protected  void drawCaps(java.awt.Graphics2D g)
           
protected  void drawFill(java.awt.Graphics2D g)
          This method is called by method draw() to draw the fill area of the figure.
protected  void drawStroke(java.awt.Graphics2D g)
          This method is called by method draw() to draw the text of the figure .
 Connector findCompatibleConnector(Connector c, boolean isStart)
          Gets a compatible connector.
 Connector findConnector(java.awt.geom.Point2D.Double p, ConnectionFigure prototype)
          Returns the Figures connector for the specified location.
 int findNode(java.awt.geom.Point2D.Double p)
          Finds a control point index.
 int findSegment(java.awt.geom.Point2D.Double find, double tolerance)
          Gets the segment of the polyline that is hit by the given Point2D.Double.
 BezierPath getBezierPath()
          Returns a clone of the bezier path of this figure.
 java.awt.geom.Rectangle2D.Double getBounds()
          Returns the untransformed logical bounds of the figure as a Rectangle.
protected  BezierPath getCappedPath()
          Returns a path which is cappedPath at the ends, to prevent it from drawing under the end caps.
 java.awt.geom.Point2D.Double getCenter()
           
 java.awt.geom.Rectangle2D.Double getDrawingArea()
          Returns the drawing area of the figure as a Rectangle.
 java.awt.geom.Point2D.Double getEndPoint()
          Convenience method for getting the end point.
 BezierPath.Node getNode(int index)
          Gets a control point.
 int getNodeCount()
          Gets the node count.
 java.awt.geom.Point2D.Double getOutermostPoint()
           
 java.awt.geom.Point2D.Double getPoint(int index)
          Convenience method for getting the point coordinate of the first control point of the specified node.
 java.awt.geom.Point2D.Double getPoint(int index, int coord)
          Gets the point coordinate of a control point.
 java.awt.geom.Point2D.Double getPointOnPath(float relative, double flatness)
           
 java.awt.geom.Point2D.Double getStartPoint()
          Convenience method for getting the start point.
 java.lang.Object getTransformRestoreData()
          Gets data which can be used to restore the transformation of the figure without loss of precision, after a transform has been applied to it.
 boolean handleMouseClick(java.awt.geom.Point2D.Double p, java.awt.event.MouseEvent evt, DrawingView view)
          Handles a mouse click.
 void invalidate()
          Invalidates cached data of the Figure.
 boolean isClosed()
           
 boolean joinSegments(java.awt.geom.Point2D.Double join, double tolerance)
          Joins two segments into one if the given Point2D.Double hits a node of the polyline.
 int joinSegments(java.awt.geom.Point2D.Double join, float tolerance)
          Joins two segments into one if the given Point2D.Double hits a node of the polyline.
 void layout()
           
 void read(DOMInput in)
           
protected  void readPoints(DOMInput in)
           
protected  void removeAllNodes()
          Removes the Point2D.Double at the specified index.
 BezierPath.Node removeNode(int index)
          Removes the Node at the specified index.
 void restoreTransformTo(java.lang.Object geometry)
          Restores the transform of the figure to a previously stored state.
<T> void
set(AttributeKey<T> key, T newValue)
          Sets an attribute of the figure.
 void setBezierPath(BezierPath newValue)
           
 void setBounds(java.awt.geom.Point2D.Double anchor, java.awt.geom.Point2D.Double lead)
          Sets the location of the first and the last BezierPath.Node of the BezierFigure.
 void setClosed(boolean newValue)
           
 void setEndPoint(java.awt.geom.Point2D.Double p)
          Convenience method for setting the point coordinate of the end point.
 void setNode(int index, BezierPath.Node p)
          Sets a control point.
 void setPoint(int index, int coord, java.awt.geom.Point2D.Double p)
          Sets the point coordinate of a control point.
 void setPoint(int index, java.awt.geom.Point2D.Double p)
          Sets the point coordinate of control point 0 at the specified node.
 void setStartPoint(java.awt.geom.Point2D.Double p)
          Convenience method for setting the point coordinate of the start point.
 int splitSegment(java.awt.geom.Point2D.Double split, double tolerance)
          Splits the segment at the given Point2D.Double if a segment was hit.
 int splitSegment(java.awt.geom.Point2D.Double split, float tolerance)
          Splits the segment at the given Point2D.Double if a segment was hit.
 void transform(java.awt.geom.AffineTransform tx)
          Transforms the shape of the Figure.
protected  void validate()
           
 void write(DOMOutput out)
           
protected  void writePoints(DOMOutput out)
           
 
Methods inherited from class org.jhotdraw.draw.AbstractAttributedFigure
applyAttributesTo, draw, drawText, get, getAttributeKey, getAttributes, getAttributesRestoreData, getStroke, getStrokeMiterLimitFactor, hasAttribute, isAttributeEnabled, readAttributes, removeAttribute, restoreAttributesTo, setAttributeEnabled, setAttributes, writeAttributes
 
Methods inherited from class org.jhotdraw.draw.AbstractFigure
addFigureListener, addNotify, basicClone, changed, findFigureInside, fireAreaInvalidated, fireAreaInvalidated, fireAreaInvalidated, fireAttributeChanged, fireFigureAdded, fireFigureChanged, fireFigureChanged, fireFigureChanged, fireFigureHandlesChanged, fireFigureRemoved, fireFigureRequestRemove, fireUndoableEditHappened, getActions, getChangingDepth, getConnectors, getCursor, getDecomposition, getDrawing, getFontRenderContext, getLayer, getLock, getPreferredSize, getTool, getToolTipText, handleDrop, includes, isChanging, isRemovable, isSelectable, isTransformable, isVisible, remap, removeFigureListener, removeNotify, requestRemove, setBounds, setRemovable, setSelectable, setTransformable, setVisible, toString, willChange
 
Methods inherited from class org.jhotdraw.beans.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, firePropertyChange, firePropertyChange, firePropertyChange, getPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

path

protected BezierPath path
The BezierPath.

Constructor Detail

BezierFigure

public BezierFigure()
Creates an empty BezierFigure, for example without any BezierPath.Nodes. The BezierFigure will not draw anything, if at least two nodes are added to it. The BezierPath created by this constructor is not closed.


BezierFigure

public BezierFigure(boolean isClosed)
Creates an empty BezierFigure, for example without any BezierPath.Nodes. The BezierFigure will not draw anything, unless at least two nodes are added to it.

Parameters:
isClosed - Specifies whether the BezierPath shall be closed.
Method Detail

findConnector

public Connector findConnector(java.awt.geom.Point2D.Double p,
                               ConnectionFigure prototype)
Returns the Figures connector for the specified location. By default a ChopDiamondConnector is returned.

Specified by:
findConnector in interface Figure
Overrides:
findConnector in class AbstractFigure
Parameters:
p - the location of the connector.
prototype - The prototype used to create a connection or null if unknown. This allows for specific connectors for different connection figures.
See Also:
ChopDiamondConnector

findCompatibleConnector

public Connector findCompatibleConnector(Connector c,
                                         boolean isStart)
Description copied from interface: Figure
Gets a compatible connector. If the provided connector is part of this figure, return the connector. If the provided connector is part of another figure, return a connector with the same semantics for this figure. Return null, if no compatible connector is available.

Specified by:
findCompatibleConnector in interface Figure
Overrides:
findCompatibleConnector in class AbstractFigure

drawStroke

protected void drawStroke(java.awt.Graphics2D g)
Description copied from class: AbstractAttributedFigure
This method is called by method draw() to draw the text of the figure . AbstractAttributedFigure configures the Graphics2D object with the TEXT_COLOR attribute before calling this method. If the TEXT_COLOR attribute is null, this method is not called.

Specified by:
drawStroke in class AbstractAttributedFigure

drawCaps

protected void drawCaps(java.awt.Graphics2D g)

drawFill

protected void drawFill(java.awt.Graphics2D g)
Description copied from class: AbstractAttributedFigure
This method is called by method draw() to draw the fill area of the figure. AbstractAttributedFigure configures the Graphics2D object with the FILL_COLOR attribute before calling this method. If the FILL_COLOR attribute is null, this method is not called.

Specified by:
drawFill in class AbstractAttributedFigure

contains

public boolean contains(java.awt.geom.Point2D.Double p)
Description copied from interface: Figure
Checks if a point is contained by the figure.

This is used for hit testing by Tool's.


canConnect

public boolean canConnect()
Checks if this figure can be connected. By default filled BezierFigures can be connected.

Specified by:
canConnect in interface Figure
Overrides:
canConnect in class AbstractFigure

createHandles

public java.util.Collection<Handle> createHandles(int detailLevel)
Description copied from interface: Figure
Creates handles used to manipulate the figure.

Specified by:
createHandles in interface Figure
Overrides:
createHandles in class AbstractFigure
Parameters:
detailLevel - The detail level of the handles. Usually this is 0 for bounding box handles and 1 for point handles. The value -1 is used by the SelectAreaTracker and the HandleTracker to highlight figures, over which the mouse pointer is hovering.
Returns:
a Collection of handles
See Also:
Handle

getBounds

public java.awt.geom.Rectangle2D.Double getBounds()
Description copied from interface: Figure
Returns the untransformed logical bounds of the figure as a Rectangle.

The bounds are used by Handle objects for adjusting the figure and for aligning the figure on a grid.


getDrawingArea

public java.awt.geom.Rectangle2D.Double getDrawingArea()
Description copied from interface: Figure
Returns the drawing area of the figure as a Rectangle.

The drawing area is used to inform DrawingView about the area that is needed to draw this figure.

The drawing area needs to be large enough, to take line width, line caps and other decorations into account that exceed the bounds of the Figure.

Specified by:
getDrawingArea in interface Figure
Overrides:
getDrawingArea in class AbstractAttributedFigure

validate

protected void validate()
Overrides:
validate in class AbstractFigure

getBezierPath

public BezierPath getBezierPath()
Returns a clone of the bezier path of this figure.


setBezierPath

public void setBezierPath(BezierPath newValue)

getPointOnPath

public java.awt.geom.Point2D.Double getPointOnPath(float relative,
                                                   double flatness)

isClosed

public boolean isClosed()

setClosed

public void setClosed(boolean newValue)

set

public <T> void set(AttributeKey<T> key,
                    T newValue)
Description copied from class: AbstractAttributedFigure
Sets an attribute of the figure. AttributeKey name and semantics are defined by the class implementing the figure interface.

Specified by:
set in interface Figure
Overrides:
set in class AbstractAttributedFigure
See Also:
AttributeKey.set(org.jhotdraw.draw.Figure, T)

setBounds

public void setBounds(java.awt.geom.Point2D.Double anchor,
                      java.awt.geom.Point2D.Double lead)
Sets the location of the first and the last BezierPath.Node of the BezierFigure. If the BezierFigure has not at least two nodes, nodes are added to the figure until the BezierFigure has at least two nodes.

Specified by:
setBounds in interface Figure
Overrides:
setBounds in class AbstractFigure
Parameters:
anchor - the start point of the bounds
lead - the end point of the bounds
See Also:
Figure.getBounds()

transform

public void transform(java.awt.geom.AffineTransform tx)
Description copied from interface: Figure
Transforms the shape of the Figure. Transformations using double precision arithmethics are inherently lossy operations. Therefore it is recommended to use getTransformRestoreData() restoreTransformTo() to provide lossless undo/redo functionality.

This is a basic operation which does not fire events. Use the following code sequence, if you need event firing:

 aFigure.willChange();
 aFigure.transform(...);
 aFigure.changed();
 

Parameters:
tx - The transformation.
See Also:
Figure.getTransformRestoreData(), Figure.restoreTransformTo(java.lang.Object)

invalidate

public void invalidate()
Description copied from class: AbstractFigure
Invalidates cached data of the Figure. This method must execute fast, because it can be called very often.

Overrides:
invalidate in class AbstractFigure

getCappedPath

protected BezierPath getCappedPath()
Returns a path which is cappedPath at the ends, to prevent it from drawing under the end caps.


layout

public void layout()

addNode

public void addNode(BezierPath.Node p)
Adds a control point.


addNode

public void addNode(int index,
                    BezierPath.Node p)
Adds a node to the list of points.


setNode

public void setNode(int index,
                    BezierPath.Node p)
Sets a control point.


getNode

public BezierPath.Node getNode(int index)
Gets a control point.


getPoint

public java.awt.geom.Point2D.Double getPoint(int index)
Convenience method for getting the point coordinate of the first control point of the specified node.


getPoint

public java.awt.geom.Point2D.Double getPoint(int index,
                                             int coord)
Gets the point coordinate of a control point.


setPoint

public void setPoint(int index,
                     java.awt.geom.Point2D.Double p)
Sets the point coordinate of control point 0 at the specified node.


setPoint

public void setPoint(int index,
                     int coord,
                     java.awt.geom.Point2D.Double p)
Sets the point coordinate of a control point.


setStartPoint

public void setStartPoint(java.awt.geom.Point2D.Double p)
Convenience method for setting the point coordinate of the start point. If the BezierFigure has not at least two nodes, nodes are added to the figure until the BezierFigure has at least two nodes.


setEndPoint

public void setEndPoint(java.awt.geom.Point2D.Double p)
Convenience method for setting the point coordinate of the end point. If the BezierFigure has not at least two nodes, nodes are added to the figure until the BezierFigure has at least two nodes.


getStartPoint

public java.awt.geom.Point2D.Double getStartPoint()
Convenience method for getting the start point.

Specified by:
getStartPoint in interface Figure
Overrides:
getStartPoint in class AbstractFigure
See Also:
Figure.setBounds(java.awt.geom.Point2D.Double, java.awt.geom.Point2D.Double)

getEndPoint

public java.awt.geom.Point2D.Double getEndPoint()
Convenience method for getting the end point.

Specified by:
getEndPoint in interface Figure
Overrides:
getEndPoint in class AbstractFigure
See Also:
Figure.setBounds(java.awt.geom.Point2D.Double, java.awt.geom.Point2D.Double)

findNode

public int findNode(java.awt.geom.Point2D.Double p)
Finds a control point index. Returns -1 if no control point could be found. FIXME - Move this to BezierPath


findSegment

public int findSegment(java.awt.geom.Point2D.Double find,
                       double tolerance)
Gets the segment of the polyline that is hit by the given Point2D.Double.

Parameters:
find - a Point on the bezier path
tolerance - a tolerance, tolerance should take into account the line width, plus 2 divided by the zoom factor.
Returns:
the index of the segment or -1 if no segment was hit.

joinSegments

public boolean joinSegments(java.awt.geom.Point2D.Double join,
                            double tolerance)
Joins two segments into one if the given Point2D.Double hits a node of the polyline.

Parameters:
join - a Point at a node on the bezier path
tolerance - a tolerance, tolerance should take into account the line width, plus 2 divided by the zoom factor.
Returns:
true if the two segments were joined.

splitSegment

public int splitSegment(java.awt.geom.Point2D.Double split,
                        double tolerance)
Splits the segment at the given Point2D.Double if a segment was hit.

Parameters:
split - a Point on (or near) a line segment on the bezier path
tolerance - a tolerance, tolerance should take into account the line width, plus 2 divided by the zoom factor.
Returns:
the index of the segment or -1 if no segment was hit.

removeNode

public BezierPath.Node removeNode(int index)
Removes the Node at the specified index.


removeAllNodes

protected void removeAllNodes()
Removes the Point2D.Double at the specified index.


getNodeCount

public int getNodeCount()
Gets the node count.


clone

public BezierFigure clone()
Description copied from interface: Figure
Returns a clone of the figure, with clones of all aggregated figures, such as children and decorators. The cloned figure does not clone the list of FigureListeners from its original.

Specified by:
clone in interface Figure
Overrides:
clone in class AbstractAttributedFigure

restoreTransformTo

public void restoreTransformTo(java.lang.Object geometry)
Description copied from interface: Figure
Restores the transform of the figure to a previously stored state.


getTransformRestoreData

public java.lang.Object getTransformRestoreData()
Description copied from interface: Figure
Gets data which can be used to restore the transformation of the figure without loss of precision, after a transform has been applied to it.

See Also:
Figure.transform(AffineTransform)

chop

public java.awt.geom.Point2D.Double chop(java.awt.geom.Point2D.Double p)

getCenter

public java.awt.geom.Point2D.Double getCenter()

getOutermostPoint

public java.awt.geom.Point2D.Double getOutermostPoint()

joinSegments

public int joinSegments(java.awt.geom.Point2D.Double join,
                        float tolerance)
Joins two segments into one if the given Point2D.Double hits a node of the polyline.

Returns:
true if the two segments were joined.

splitSegment

public int splitSegment(java.awt.geom.Point2D.Double split,
                        float tolerance)
Splits the segment at the given Point2D.Double if a segment was hit.

Returns:
the index of the segment or -1 if no segment was hit.

handleMouseClick

public boolean handleMouseClick(java.awt.geom.Point2D.Double p,
                                java.awt.event.MouseEvent evt,
                                DrawingView view)
Handles a mouse click.

Specified by:
handleMouseClick in interface Figure
Overrides:
handleMouseClick in class AbstractFigure
Parameters:
p - The location of the mouse event.
evt - The mouse event.
view - The drawing view which is the source of the mouse event.
Returns:
Returns true, if the event was consumed.

write

public void write(DOMOutput out)
           throws java.io.IOException
Specified by:
write in interface DOMStorable
Overrides:
write in class AbstractAttributedFigure
Throws:
java.io.IOException

writePoints

protected void writePoints(DOMOutput out)
                    throws java.io.IOException
Throws:
java.io.IOException

read

public void read(DOMInput in)
          throws java.io.IOException
Specified by:
read in interface DOMStorable
Overrides:
read in class AbstractAttributedFigure
Throws:
java.io.IOException

readPoints

protected void readPoints(DOMInput in)
                   throws java.io.IOException
Throws:
java.io.IOException

Copyright 1996-2009 (c) by the authors and contributors of the JHotDraw project.
Some rights reserved.