Class PPath

java.lang.Object
edu.umd.cs.piccolo.PNode
edu.umd.cs.piccolo.nodes.PPath
All Implemented Interfaces:
Printable, Serializable, Cloneable

public class PPath extends PNode
PPath is a wrapper around a java.awt.geom.GeneralPath. The setBounds method works by scaling the path to fit into the specified bounds. This normally works well, but if the specified base bounds get too small then it is impossible to expand the path shape again since all its numbers have tended to zero, so application code may need to take this into consideration.

One option that applications have is to call startResizeBounds before starting an interaction that may make the bounds very small, and calling endResizeBounds when this interaction is finished. When this is done PPath will use a copy of the original path to do the resizing so the numbers in the path wont loose resolution.

This class also provides methods for constructing common shapes using a general path.

Version:
1.0
Author:
Jesse Grosjean
See Also:
  • Field Details

    • PROPERTY_STROKE_PAINT

      public static final String PROPERTY_STROKE_PAINT
      The property name that identifies a change of this node's stroke paint (see getStrokePaint). Both old and new value will be set correctly to Paint objects in any property change event.
      See Also:
    • PROPERTY_CODE_STROKE_PAINT

      public static final int PROPERTY_CODE_STROKE_PAINT
      The property code that identifies a change of this node's stroke paint (see getStrokePaint). Both old and new value will be set correctly to Paint objects in any property change event.
      See Also:
    • PROPERTY_STROKE

      public static final String PROPERTY_STROKE
      The property name that identifies a change of this node's stroke (see getStroke). Both old and new value will be set correctly to Stroke objects in any property change event.
      See Also:
    • PROPERTY_CODE_STROKE

      public static final int PROPERTY_CODE_STROKE
      The property code that identifies a change of this node's stroke (see getStroke). Both old and new value will be set correctly to Stroke objects in any property change event.
      See Also:
    • PROPERTY_PATH

      public static final String PROPERTY_PATH
      The property name that identifies a change of this node's path (see getPathReference). In any property change event the new value will be a reference to this node's path, but old value will always be null.
      See Also:
    • PROPERTY_CODE_PATH

      public static final int PROPERTY_CODE_PATH
      The property code that identifies a change of this node's path (see getPathReference). In any property change event the new value will be a reference to this node's path, but old value will always be null.
      See Also:
  • Constructor Details

    • PPath

      public PPath()
      Creates an empty PPath with the default paint and stroke.
    • PPath

      public PPath(Shape aShape)
      Creates an PPath in the given shape with the default paint and stroke.
      Parameters:
      aShape - the desired shape
    • PPath

      public PPath(Shape aShape, Stroke aStroke)
      Construct this path with the given shape and stroke. This method may be used to optimize the creation of a large number of PPaths. Normally PPaths have a default stroke of width one, but when a path has a non null stroke it takes significantly longer to compute its bounds. This method allows you to override that default stroke before the bounds are ever calculated, so if you pass in a null stroke here you won't ever have to pay that bounds calculation price if you don't need to.
      Parameters:
      aShape - desired shape or null if you desire an empty path
      aStroke - desired stroke
  • Method Details

    • createRectangle

      public static PPath createRectangle(float x, float y, float width, float height)
      Creates a PPath object in the shape of a rectangle.
      Parameters:
      x - left of the rectangle
      y - top of the rectangle
      width - width of the rectangle
      height - height of the rectangle
      Returns:
      created rectangle
    • createRoundRectangle

      public static PPath createRoundRectangle(float x, float y, float width, float height, float arcWidth, float arcHeight)
      Creates a PPath object in the shape of a rounded rectangle.
      Parameters:
      x - left of the rectangle
      y - top of the rectangle
      width - width of the rectangle
      height - height of the rectangle
      arcWidth - the arc width at the corners of the rectangle
      arcHeight - the arc height at the corners of the rectangle
      Returns:
      created rounded rectangle
    • createEllipse

      public static PPath createEllipse(float x, float y, float width, float height)
      Creates a PPath object in the shape of an ellipse.
      Parameters:
      x - left of the ellipse
      y - top of the ellipse
      width - width of the ellipse
      height - height of the ellipse
      Returns:
      created ellipse
    • createLine

      public static PPath createLine(float x1, float y1, float x2, float y2)
      Creates a PPath in the shape of a line.
      Parameters:
      x1 - x component of the first point
      y1 - y component of the first point
      x2 - x component of the second point
      y2 - y component of the second point
      Returns:
      created line
    • createPolyline

      public static PPath createPolyline(Point2D[] points)
      Creates a PPath for the poly-line for the given points.
      Parameters:
      points - array of points for the point lines
      Returns:
      created poly-line for the given points
    • createPolyline

      public static PPath createPolyline(float[] xp, float[] yp)
      Creates a PPath for the poly-line for the given points.
      Parameters:
      xp - array of x components of the points of the poly-lines
      yp - array of y components of the points of the poly-lines
      Returns:
      created poly-line for the given points
    • getStrokePaint

      public Paint getStrokePaint()
      Returns the stroke paint of the PPath.
      Returns:
      stroke paint of the PPath
    • setStrokePaint

      public void setStrokePaint(Paint newStrokePaint)
      Sets the stroke paint of the path.
      Parameters:
      newStrokePaint - the paint to use as this path's stroke paint
    • getStroke

      public Stroke getStroke()
      Returns the stroke to use when drawing the path.
      Returns:
      current stroke of path
    • setStroke

      public void setStroke(Stroke aStroke)
      Sets the stroke to use when drawing the path.
      Parameters:
      aStroke - stroke to use when drawing the path
    • startResizeBounds

      public void startResizeBounds()
      Stores the original size of the path before resizing started.
      Overrides:
      startResizeBounds in class PNode
    • endResizeBounds

      public void endResizeBounds()
      Clears the size of the path before resizing.
      Overrides:
      endResizeBounds in class PNode
    • internalUpdateBounds

      protected void internalUpdateBounds(double x, double y, double width, double height)
      Set the bounds of this path. This method works by scaling the path to fit into the specified bounds. This normally works well, but if the specified base bounds get too small then it is impossible to expand the path shape again since all its numbers have tended to zero, so application code may need to take this into consideration.
      Overrides:
      internalUpdateBounds in class PNode
      Parameters:
      x - new left position of bounds
      y - new top position of bounds
      width - the new width of the bounds
      height - the new height of the bounds
    • intersects

      public boolean intersects(Rectangle2D aBounds)
      Returns true if path crosses the provided bounds. Takes visibility of path into account.
      Overrides:
      intersects in class PNode
      Parameters:
      aBounds - bounds being tested for intersection
      Returns:
      true if path visibly crosses bounds
    • getPathBoundsWithStroke

      public Rectangle2D getPathBoundsWithStroke()
      Calculates the path's bounds taking stroke into account.
      Returns:
      bounds of the path taking stroke width into account
    • updateBoundsFromPath

      public void updateBoundsFromPath()
      Recomputes the bounds taking stroke into account.
    • paint

      protected void paint(PPaintContext paintContext)
      Paints the path in the provided paintContext. Can perform very differently depending on whether the path is being drawn using its stroke or its paint. It both are provided to the path, fun ensues.
      Overrides:
      paint in class PNode
      Parameters:
      paintContext - context in which painting is occurring
    • getPathReference

      public GeneralPath getPathReference()
      Provides direct access to the underlying GeneralPath object.
      Returns:
      underlying GeneralPath
    • moveTo

      public void moveTo(float x, float y)
      Appends a "move" operation to the end of the path.
      Parameters:
      x - the x component of the point to move to
      y - the y component of the point to move to
    • lineTo

      public void lineTo(float x, float y)
      Draws a line from the last point in the path to point provided.
      Parameters:
      x - the x component of the point
      y - the y component of the point
    • quadTo

      public void quadTo(float x1, float y1, float x2, float y2)
      Adds a curved segment, defined by two new points, to the path by drawing a Quadratic curve that intersects both the current coordinates and the coordinates (x2, y2), using the specified point (x1, y1) as a quadratic parametric control point.
      Parameters:
      x1 - x component of quadratic parametric control point
      y1 - y component of quadratic parametric control point
      x2 - x component of point through which quad curve will pass
      y2 - y component of point through which quad curve will pass
    • curveTo

      public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3)
      Adds a curved segment, defined by three new points, to the path by drawing a Bézier curve that intersects both the current coordinates and the coordinates (x3, y3), using the specified points (x1, y1) and (x2, y2) as Bézier control points.
      Parameters:
      x1 - x component of first Bézier control point
      y1 - y component of first Bézier control point
      x2 - x component of second Bézier control point
      y2 - y component of second Bézier control point
      x3 - x component of point through which curve must pass
      y3 - y component of point through which curve must pass
    • append

      public void append(Shape aShape, boolean connect)
      Appends the provided shape to the end of this path, it may conditionally connect them together if they are disjoint.
      Parameters:
      aShape - shape to append
      connect - whether to perform a lineTo operation to the beginning of the shape before appending
    • setPathTo

      public void setPathTo(Shape aShape)
      Replaces this PPath's path with the one provided.
      Parameters:
      aShape - shape to replace the current one with
    • setPathToRectangle

      public void setPathToRectangle(float x, float y, float width, float height)
      Resets the path to a rectangle with the dimensions and position provided.
      Parameters:
      x - left of the rectangle
      y - top of te rectangle
      width - width of the rectangle
      height - height of the rectangle
    • setPathToEllipse

      public void setPathToEllipse(float x, float y, float width, float height)
      Resets the path to an ellipse positioned at the coordinate provided with the dimensions provided.
      Parameters:
      x - left of the ellipse
      y - top of the ellipse
      width - width of the ellipse
      height - height of the ellipse
    • setPathToPolyline

      public void setPathToPolyline(Point2D[] points)
      Sets the path to a sequence of segments described by the points.
      Parameters:
      points - points to that lie along the generated path
    • setPathToPolyline

      public void setPathToPolyline(float[] xp, float[] yp)
      Sets the path to a sequence of segments described by the point components provided.
      Parameters:
      xp - the x components of the points along the path
      yp - the y components of the points along the path
    • closePath

      public void closePath()
      Marks the path as closed. Making changes to it impossible.
    • reset

      public void reset()
      Empties the path.