nodebox.graphics
Class Path

java.lang.Object
  extended by nodebox.graphics.AbstractTransformable
      extended by nodebox.graphics.AbstractGeometry
          extended by nodebox.graphics.Path
All Implemented Interfaces:
Cloneable, Iterable<Point>, Colorizable, Drawable, Grob, IGeometry

public class Path
extends AbstractGeometry
implements Colorizable, Iterable<Point>

Base class for all geometric (vector) data.


Field Summary
 
Fields inherited from interface nodebox.graphics.IGeometry
DEFAULT_POINT_AMOUNT
 
Constructor Summary
Path()
           
Path(Contour c)
           
Path(Path other)
           
Path(Path other, boolean cloneContours)
           
Path(Shape s)
           
 
Method Summary
 void add(Contour c)
          Add the given contour.
 void addPoint(double x, double y)
          Add a new point to the geometry specified by its x and y coordinates.
 void addPoint(Point pt)
          Add the given point to the geometry.
 Geometry asGeometry()
          Wrap the current path in a geometry object.
 void clear()
           
 Path clone()
          Clone the geometry, returning a new copy that is totally independent from the original.
 Path cloneAndClear()
           
 void close()
           
 boolean contains(double x, double y)
           
 boolean contains(Point p)
           
 boolean contains(Rect r)
           
 Contour contourAt(double t)
           
 void cornerEllipse(double x, double y, double width, double height)
           
 void cornerRect(double x, double y, double width, double height)
           
 void cornerRect(double cx, double cy, double width, double height, double r)
           
 void cornerRect(double cx, double cy, double width, double height, double rx, double ry)
           
 void cornerRect(Rect r)
           
 void cornerRect(Rect r, double roundness)
           
 void cornerRect(Rect r, double rx, double ry)
           
static double curveLength(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3)
          Returns the length of the spline.
static double curveLength(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, int n)
          Returns the length of the spline.
static Point curvePoint(double t, double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3)
          Returns coordinates for point at t on the spline.
 void curveto(double x1, double y1, double x2, double y2, double x3, double y3)
           
 void draw(Graphics2D g)
           
 void ellipse(double cx, double cy, double width, double height)
          Add an ellipse shape to the path.
 void extend(Path p)
           
 void extend(Shape s)
           
static Path findPath(List<Point> points)
           
static Path findPath(List<Point> points, double curvature)
           
static Path findPath(Point[] points)
           
static Path findPath(Point[] points, double curvature)
          Constructs a path between the given list of points.
 void flatten()
          Flatten the geometry.
 Path flattened()
          Make a flattened copy of the geometry.
 Rect getBounds()
           
 List<Contour> getContours()
          Get the contours of a geometry object.
 Color getFill()
           
 Color getFillColor()
           
 GeneralPath getGeneralPath()
           
 double getLength()
          Calculate the length of the path.
 int getPointCount()
           
 List<Point> getPoints()
          Get the points for this geometry.
 Color getStroke()
           
 Color getStrokeColor()
           
 double getStrokeWidth()
           
 Path intersected(Path p)
           
 boolean intersects(Path p)
           
 boolean intersects(Rect r)
           
 void invalidate()
          Invalidates the cache.
 boolean isClosed()
          Check if the last contour on this path is closed.
 boolean isEmpty()
           
 Iterator<Point> iterator()
           
 void line(double x1, double y1, double x2, double y2)
           
static double lineLength(double x0, double y0, double x1, double y1)
          Returns the length of the line.
static Point linePoint(double t, double x0, double y0, double x1, double y1)
          Returns coordinates for point at t on the line.
 void lineto(double x, double y)
           
 Point[] makePoints(int amount, boolean perContour)
          Make new points along the contours of the existing path.
 AbstractGeometry mapPoints(com.google.common.base.Function<Point,Point> pointFunction)
          Change all points in the geometry and return a mutated copy.
 void moveto(double x, double y)
           
 void newContour()
          Start a new contour without closing the current contour first.
 Point point(double t)
          Same as pointAt(t).
 Point pointAt(double t)
          Returns coordinates for point at t on the path.
 void rect(double cx, double cy, double width, double height)
          Add a rectangle shape to the path.
 void rect(double cx, double cy, double width, double height, double r)
           
 void rect(double cx, double cy, double width, double height, double rx, double ry)
           
 void rect(Rect r)
           
 void rect(Rect r, double roundness)
           
 void rect(Rect r, double rx, double ry)
           
 Path resampleByAmount(int amount, boolean perContour)
          Generate new geometry with the given amount of points along the shape of the original geometry.
 Path resampleByLength(double segmentLength)
          Generate new geometry with points along the shape of the original geometry, spaced at the given length.
 void roundedRect(double cx, double cy, double width, double height, double r)
           
 void roundedRect(double cx, double cy, double width, double height, double rx, double ry)
           
 void roundedRect(Rect r, double roundness)
           
 void roundedRect(Rect r, double rx, double ry)
           
 void setFill(Color c)
           
 void setFillColor(Color fillColor)
           
 void setStroke(Color c)
           
 void setStrokeColor(Color strokeColor)
           
 void setStrokeWidth(double strokeWidth)
           
 int size()
           
 Path subtracted(Path p)
           
 void text(Text t)
           
 String toString()
           
 void transform(Transform t)
           
 Path united(Path p)
           
 
Methods inherited from class nodebox.graphics.AbstractGeometry
extend, extend, makePoints, makePoints, pointDelta
 
Methods inherited from class nodebox.graphics.AbstractTransformable
getTransformDelegate, rotate, rotateRadians, scale, scale, setTransformDelegate, skew, skew, translate
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface nodebox.graphics.Grob
getTransformDelegate, rotate, rotateRadians, scale, scale, setTransformDelegate, skew, skew, translate
 

Constructor Detail

Path

public Path()

Path

public Path(Path other)

Path

public Path(Path other,
            boolean cloneContours)

Path

public Path(Shape s)

Path

public Path(Contour c)
Method Detail

asGeometry

public Geometry asGeometry()
Wrap the current path in a geometry object.

Returns:
a Geometry object

getFillColor

public Color getFillColor()

getFill

public Color getFill()

setFillColor

public void setFillColor(Color fillColor)
Specified by:
setFillColor in interface Colorizable

setFill

public void setFill(Color c)
Specified by:
setFill in interface Colorizable

getStrokeColor

public Color getStrokeColor()

getStroke

public Color getStroke()

setStrokeColor

public void setStrokeColor(Color strokeColor)
Specified by:
setStrokeColor in interface Colorizable

setStroke

public void setStroke(Color c)
Specified by:
setStroke in interface Colorizable

getStrokeWidth

public double getStrokeWidth()

setStrokeWidth

public void setStrokeWidth(double strokeWidth)
Specified by:
setStrokeWidth in interface Colorizable

getPointCount

public int getPointCount()
Specified by:
getPointCount in interface IGeometry

getPoints

public List<Point> getPoints()
Get the points for this geometry.

This returns a live reference to the points of the geometry. Changing the points will change the geometry.

Specified by:
getPoints in interface IGeometry
Returns:
a list of Points.

moveto

public void moveto(double x,
                   double y)

lineto

public void lineto(double x,
                   double y)

curveto

public void curveto(double x1,
                    double y1,
                    double x2,
                    double y2,
                    double x3,
                    double y3)

close

public void close()

newContour

public void newContour()
Start a new contour without closing the current contour first.

You can call this method even when there is no current contour.


addPoint

public void addPoint(Point pt)
Description copied from interface: IGeometry
Add the given point to the geometry. The point is cloned.

Specified by:
addPoint in interface IGeometry
Parameters:
pt - the point to add.

addPoint

public void addPoint(double x,
                     double y)
Description copied from interface: IGeometry
Add a new point to the geometry specified by its x and y coordinates.

Specified by:
addPoint in interface IGeometry
Parameters:
x - the X coordinate.
y - the Y coordinate.

invalidate

public void invalidate()
Invalidates the cache. Querying the path length or asking for getGeneralPath will return an up-to-date result.

This operation recursively invalidates all underlying geometry.

Cache invalidation happens automatically when using the Path methods, such as rect/ellipse, or container operations such as add/extend/clear. You should invalidate the cache when manually changing the point positions or adding points to the underlying contours.

Invalidating the cache is a lightweight operation; it doesn't recalculate anything. Only when querying the new length will the values be recalculated.


rect

public void rect(Rect r)

rect

public void rect(double cx,
                 double cy,
                 double width,
                 double height)
Add a rectangle shape to the path. The rectangle will be centered around the x,y coordinates.

Parameters:
cx - the horizontal center of the rectangle
cy - the vertical center of the rectangle
width - the width
height - the height

rect

public void rect(Rect r,
                 double roundness)

rect

public void rect(Rect r,
                 double rx,
                 double ry)

rect

public void rect(double cx,
                 double cy,
                 double width,
                 double height,
                 double r)

rect

public void rect(double cx,
                 double cy,
                 double width,
                 double height,
                 double rx,
                 double ry)

cornerRect

public void cornerRect(double x,
                       double y,
                       double width,
                       double height)

cornerRect

public void cornerRect(Rect r)

cornerRect

public void cornerRect(Rect r,
                       double roundness)

cornerRect

public void cornerRect(Rect r,
                       double rx,
                       double ry)

cornerRect

public void cornerRect(double cx,
                       double cy,
                       double width,
                       double height,
                       double r)

cornerRect

public void cornerRect(double cx,
                       double cy,
                       double width,
                       double height,
                       double rx,
                       double ry)

roundedRect

public void roundedRect(Rect r,
                        double roundness)

roundedRect

public void roundedRect(Rect r,
                        double rx,
                        double ry)

roundedRect

public void roundedRect(double cx,
                        double cy,
                        double width,
                        double height,
                        double r)

roundedRect

public void roundedRect(double cx,
                        double cy,
                        double width,
                        double height,
                        double rx,
                        double ry)

ellipse

public void ellipse(double cx,
                    double cy,
                    double width,
                    double height)
Add an ellipse shape to the path. The ellipse will be centered around the x,y coordinates.

Parameters:
cx - the horizontal center of the ellipse
cy - the vertical center of the ellipse
width - the width
height - the height

cornerEllipse

public void cornerEllipse(double x,
                          double y,
                          double width,
                          double height)

line

public void line(double x1,
                 double y1,
                 double x2,
                 double y2)

text

public void text(Text t)

add

public void add(Contour c)
Add the given contour. This will also make it active, so all new drawing operations will operate on the given contour.

The given contour is not cloned.

Parameters:
c - the contour to add.

size

public int size()

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Grob

clear

public void clear()

extend

public void extend(Path p)

extend

public void extend(Shape s)

getContours

public List<Contour> getContours()
Get the contours of a geometry object.

This method returns live references to the geometric objects. Changing them will change the original geometry.

Returns:
a list of contours

isClosed

public boolean isClosed()
Check if the last contour on this path is closed.

A path can't technically be called "closed", only specific contours in the path can. This method provides a reasonable heuristic for a "closed" path by checking the closed state of the last contour. It returns false if this path contains no contours.

Returns:
true if the last contour is closed.

lineLength

public static double lineLength(double x0,
                                double y0,
                                double x1,
                                double y1)
Returns the length of the line.

Parameters:
x0 - X start coordinate
y0 - Y start coordinate
x1 - X end coordinate
y1 - Y end coordinate
Returns:
the length of the line

linePoint

public static Point linePoint(double t,
                              double x0,
                              double y0,
                              double x1,
                              double y1)
Returns coordinates for point at t on the line.

Calculates the coordinates of x and y for a point at t on a straight line.

The t port is a number between 0.0 and 1.0, x0 and y0 define the starting point of the line, x1 and y1 the ending point of the line,

Parameters:
t - a number between 0.0 and 1.0 defining the position on the path.
x0 - X start coordinate
y0 - Y start coordinate
x1 - X end coordinate
y1 - Y end coordinate
Returns:
a Point at position t on the line.

curveLength

public static double curveLength(double x0,
                                 double y0,
                                 double x1,
                                 double y1,
                                 double x2,
                                 double y2,
                                 double x3,
                                 double y3)
Returns the length of the spline.

Integrates the estimated length of the cubic bezier spline defined by x0, y0, ... x3, y3, by adding the lengths of linear lines between points at t.

The number of points is defined by n (n=10 would add the lengths of lines between 0.0 and 0.1, between 0.1 and 0.2, and so on).

This will use a default accuracy of 20, which is fine for most cases, usually resulting in a deviation of less than 0.01.

Parameters:
x0 - X start coordinate
y0 - Y start coordinate
x1 - X control point 1
y1 - Y control point 1
x2 - X control point 2
y2 - Y control point 2
x3 - X end coordinate
y3 - Y end coordinate
Returns:
the length of the spline.

curveLength

public static double curveLength(double x0,
                                 double y0,
                                 double x1,
                                 double y1,
                                 double x2,
                                 double y2,
                                 double x3,
                                 double y3,
                                 int n)
Returns the length of the spline.

Integrates the estimated length of the cubic bezier spline defined by x0, y0, ... x3, y3, by adding the lengths of linear lines between points at t.

The number of points is defined by n (n=10 would add the lengths of lines between 0.0 and 0.1, between 0.1 and 0.2, and so on).

Parameters:
x0 - X start coordinate
y0 - Y start coordinate
x1 - X control point 1
y1 - Y control point 1
x2 - X control point 2
y2 - Y control point 2
x3 - X end coordinate
y3 - Y end coordinate
n - accuracy
Returns:
the length of the spline.

curvePoint

public static Point curvePoint(double t,
                               double x0,
                               double y0,
                               double x1,
                               double y1,
                               double x2,
                               double y2,
                               double x3,
                               double y3)
Returns coordinates for point at t on the spline.

Calculates the coordinates of x and y for a point at t on the cubic bezier spline, and its control points, based on the de Casteljau interpolation algorithm.

Parameters:
t - a number between 0.0 and 1.0 defining the position on the path.
x0 - X start coordinate
y0 - Y start coordinate
x1 - X control point 1
y1 - Y control point 1
x2 - X control point 2
y2 - Y control point 2
x3 - X end coordinate
y3 - Y end coordinate
Returns:
a Point at position t on the spline.

getLength

public double getLength()
Calculate the length of the path. This is not the number of segments, but rather the sum of all segment lengths.

Returns:
the length of the path.

contourAt

public Contour contourAt(double t)

pointAt

public Point pointAt(double t)
Returns coordinates for point at t on the path.

Gets the length of the path, based on the length of each curve and line in the path. Determines in what segment t falls. Gets the point on that segment.

Specified by:
pointAt in class AbstractGeometry
Parameters:
t - relative coordinate of the point (between 0.0 and 1.0) Results outside of this range are undefined.
Returns:
coordinates for point at t.

point

public Point point(double t)
Same as pointAt(t).

This method is here for compatibility with NodeBox 1.

Parameters:
t - relative coordinate of the point.
Returns:
coordinates for point at t.
See Also:
pointAt(double)

makePoints

public Point[] makePoints(int amount,
                          boolean perContour)
Make new points along the contours of the existing path.

Points are evenly distributed according to the length of each contour.

Specified by:
makePoints in interface IGeometry
Parameters:
amount - the number of points to create.
perContour - if true, the amount of points is generated per contour, otherwise the amount is for the entire path.
Returns:
a list of Points.

resampleByAmount

public Path resampleByAmount(int amount,
                             boolean perContour)
Description copied from interface: IGeometry
Generate new geometry with the given amount of points along the shape of the original geometry.

The length of each segment is not given and will be determined based on the required number of points.

Specified by:
resampleByAmount in interface IGeometry
Parameters:
amount - the number of points to generate.
perContour - whether the given points are per contour, or for the entire geometry.
Returns:
a new geometry object. This method will return whatever comes out of it, so calling resample on a Contour will return a new Contour object.

resampleByLength

public Path resampleByLength(double segmentLength)
Description copied from interface: IGeometry
Generate new geometry with points along the shape of the original geometry, spaced at the given length.

The number of points is not given and will be determined by the system based on the segment length.

Specified by:
resampleByLength in interface IGeometry
Parameters:
segmentLength - the maximum length of each resampled segment.
Returns:
a new geometry object. This object will be of the same type as the callee, so calling resample on a Contour will return a new Contour object.

findPath

public static Path findPath(List<Point> points)

findPath

public static Path findPath(List<Point> points,
                            double curvature)

findPath

public static Path findPath(Point[] points)

findPath

public static Path findPath(Point[] points,
                            double curvature)
Constructs a path between the given list of points.

Interpolates the list of points and determines a smooth bezier path betweem them. Curvature is only useful if the path has more than three points.

Parameters:
points - the points of which to construct the path from.
curvature - the smoothness of the generated path (0: straight, 1: smooth)
Returns:
a new Path.

contains

public boolean contains(Point p)

contains

public boolean contains(double x,
                        double y)

contains

public boolean contains(Rect r)

intersects

public boolean intersects(Rect r)

intersects

public boolean intersects(Path p)

intersected

public Path intersected(Path p)

subtracted

public Path subtracted(Path p)

united

public Path united(Path p)

getGeneralPath

public GeneralPath getGeneralPath()

getBounds

public Rect getBounds()
Specified by:
getBounds in interface Grob

transform

public void transform(Transform t)
Specified by:
transform in interface Grob

flatten

public void flatten()
Flatten the geometry.

Specified by:
flatten in interface IGeometry

flattened

public Path flattened()
Make a flattened copy of the geometry.

Specified by:
flattened in interface IGeometry
Returns:
a flattened copy.

draw

public void draw(Graphics2D g)
Specified by:
draw in interface Drawable

clone

public Path clone()
Description copied from interface: IGeometry
Clone the geometry, returning a new copy that is totally independent from the original.

Specified by:
clone in interface Colorizable
Specified by:
clone in interface Grob
Specified by:
clone in interface IGeometry
Specified by:
clone in class AbstractGeometry
Returns:
the new geometry object.

cloneAndClear

public Path cloneAndClear()

mapPoints

public AbstractGeometry mapPoints(com.google.common.base.Function<Point,Point> pointFunction)
Description copied from class: AbstractGeometry
Change all points in the geometry and return a mutated copy. The original geometry remains unchanged.

Specified by:
mapPoints in class AbstractGeometry
Parameters:
pointFunction - The function to apply to each point.
Returns:
The new geometry.

iterator

public Iterator<Point> iterator()
Specified by:
iterator in interface Iterable<Point>

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2013. All Rights Reserved.