public final class GeneralPath

  1. Object
  2. GeneralPath

ImplementsShape

A general geometric path, consisting of any number of subpaths constructed out of straight lines and cubic or quadratic Bezier curves. The inside of the curve is defined for drawing purposes by a winding rule. Either the #WIND_EVEN_ODD or #WIND_NON_ZERO winding rule can be chosen.

A drawing of a GeneralPath

The #WIND_EVEN_ODD winding rule defines a point as inside a path if: A ray from the point towards infinity in an arbitrary direction intersects the path an odd number of times. Points A and C in the image are considered to be outside the path. (both intersect twice) Point B intersects once, and is inside.

The #WIND_NON_ZERO winding rule defines a point as inside a path if: The path intersects the ray in an equal number of opposite directions. Point A in the image is outside (one intersection in the ‘up’ direction, one in the ‘down’ direction) Point B in the image is inside (one intersection ‘down’) Point C in the image is inside (two intersections in the ‘down’ direction)

Form hi = new Form("Shape");

// We create a 50 x 100 shape, this is arbitrary since we can scale it easily
GeneralPath path = new GeneralPath();
path.moveTo(20,0);
path.lineTo(30, 0);
path.lineTo(30, 100);
path.lineTo(20, 100);
path.lineTo(20, 15);
path.lineTo(5, 40);
path.lineTo(5, 25);
path.lineTo(20,0);

hi.getContentPane().getUnselectedStyle().setBgPainter(new Painter() {
    public void paint(Graphics g, Rectangle rect) {
        g.setColor(0xff);
        float widthRatio = ((float) rect.getWidth()) / 50f;
        float heightRatio = ((float) rect.getHeight()) / 100f;
        g.scale(widthRatio, heightRatio);
        g.translate((int) (((float) rect.getX()) / widthRatio), (int) (((float) rect.getY()) / heightRatio));
        g.fillShape(path);
        g.resetAffine();
    }
});

hi.show();

Note: This description and image were copied from the GNU classpath docs). License here http://www.gnu.org/licenses/licenses.html#FDL

Fields

public static final int WIND_EVEN_ODD = 0Same constant as PathIterator#WIND_EVEN_ODD
public static final int WIND_NON_ZERO = 1Same constant as PathIterator#WIND_NON_ZERO

Constructors

public GeneralPath()Constructs a GeneralPath with the default (#WIND_NON_ZERO) winding rule and initial capacity (10).
public GeneralPath(int rule)Constructs a GeneralPath with a specific winding rule and the default initial capacity (10).
public GeneralPath(int rule, int initialCapacity)Constructs a GeneralPath with a specific winding rule and the initial capacity.
public GeneralPath(Shape shape)Constructs a GeneralPath from an arbitrary shape object.

Methods

public static synchronized void recycle(GeneralPath p)Returns a GeneralPath to the reusable object pool for GeneralPaths.
public static GeneralPath createFromPool()Creates a new GeneralPath from an object Pool.
public static boolean isConvexPolygon(float[] xPoints, float[] yPoints)Checks to see if the set of points form a convex polygon.
public static boolean isConvexPolygon(int[] xPoints, int[] yPoints)
public boolean isPolygon()Checks to see if this path forms a polygon.
public int getTypesSize()Returns the number of path commands in this path.
public int getPointsSize()Returns the number of points in this path.
public void getTypes(byte[] out)Returns a copy of the types (aka path commands) in this path.
public void getPoints(float[] out)Returns a copy of the points in this path.
public String toString()Returns a string representation of the object.
public boolean equals(Shape shape, Transform t)
public int getWindingRule()Returns the path’s current winding rule.
public void setWindingRule(int rule)Sets the path’s winding rule, which controls which areas are considered ‘inside’ or ‘outside’ the path on drawing.
public void moveTo(double x, double y)
public void moveTo(float x, float y)Adds a new point to a path.
public void lineTo(double x, double y)
public void lineTo(float x, float y)Appends a straight line to the current path.
public void quadTo(double x1, double y1, double x2, double y2)
public void quadTo(float x1, float y1, float x2, float y2)Appends a quadratic Bezier curve to the current path.
public void curveTo(double x1, double y1, double x2, double y2, double x3, double y3)
public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3)Appends a cubic Bezier curve to the current path.
public void arc(float x, float y, float w, float h, float startAngle, float sweepAngle)Draws an elliptical arc on the path given the provided bounds.
public void arc(float x, float y, float w, float h, float startAngle, float sweepAngle, boolean joinPath)Draws an elliptical arc on the path given the provided bounds.
public void arc(double x, double y, double w, double h, double startAngle, double sweepAngle)Draws an elliptical arc on the path given the provided bounds.
public void arc(double x, double y, double w, double h, double startAngle, double sweepAngle, boolean joinPath)Draws an elliptical arc on the path given the provided bounds.
public void arcTo(float cX, float cY, float endX, float endY)Adds an arc to the path.
public void arcTo(float cX, float cY, float endX, float endY, boolean clockwise)Adds an arc to the path.
public void arcTo(double cX, double cY, double endX, double endY)Adds an arc to the path.
public void arcTo(double cX, double cY, double endX, double endY, boolean clockwise)Adds an arc to the path.
public void closePath()Closes the current subpath by drawing a line back to the point of the last moveTo, unless the path is already closed.
public void append(Shape shape, boolean connect)Appends the segments of a Shape to the path.
public void append(PathIterator path, boolean connect)Appends the segments of a PathIterator to this GeneralPath.
public float[] getCurrentPoint()Returns the current appending point of the path.
public void getCurrentPoint(float[] point)Sets the coordinates of the given point to the current point in the path.
public void reset()Resets the path.
public float[] getBounds2D()Returns the path’s bounding box, in float precision.
public void getBounds2D(float[] out)Sets the 4-element array to the bounding box coordinates of the path.
public Rectangle getBounds()Returns the path’s bounding box.
public void getBounds(Rectangle out)Sets the coordinates of the provided rectangle to the bounding box of this path.
public boolean isRectangle()Checks to see if this path is a rectangle.
public PathIterator getPathIterator(){Gets an iterator to walk all of the path segments of the shape.}
public PathIterator getPathIterator(Transform m){Gets an iterator where all points are transformed by the provided transform.
public Shape createTransformedShape(Transform m)Returns a shape formed by transforming the current shape with the provided transform.
public void setPath(GeneralPath p, Transform t)Sets this path to be identical to the provided path p with the given Transform t applied to it.
public void setRect(Rectangle r, Transform t)Sets this path to be a rectangle with the provided bounds, but with the given transform applied to it.
public void setShape(Shape s, Transform t)Sets this path to be a copy of the provided shape, but with the provided transform applied to it.
public boolean intersect(Rectangle rect)Sets the current path to the intersection of itself and the provided rectangle.
public boolean intersect(int x, int y, int w, int h)
public void transform(Transform m)Transforms the current path in place using the given transform.
public void intersect(Shape shape)Resets this path to be the intersection of itself with the given shape.
public Shape intersection(Rectangle rect){Returns the shape formed by the intersection of this shape and the provided rectangle.}
public boolean contains(float x, float y)Checks if the given point is contained in the current shape.
public boolean contains(int x, int y){Checks if the shape contains the given point.}

Inherited methods

Field details

WIND_EVEN_ODD

public static final int WIND_EVEN_ODD = 0
Same constant as PathIterator#WIND_EVEN_ODD

WIND_NON_ZERO

public static final int WIND_NON_ZERO = 1
Same constant as PathIterator#WIND_NON_ZERO

Constructor details

GeneralPath

public GeneralPath()
Constructs a GeneralPath with the default (#WIND_NON_ZERO) winding rule and initial capacity (10).

GeneralPath

public GeneralPath(int rule)
Constructs a GeneralPath with a specific winding rule and the default initial capacity (10).

Parameters

rule int
The winding rule. One of #WIND_NON_ZERO and #WIND_EVEN_ODD

GeneralPath

public GeneralPath(int rule, int initialCapacity)
Constructs a GeneralPath with a specific winding rule and the initial capacity. The initial capacity should be the approximate number of path segments to be used.

Parameters

rule int
The winding rule. (#WIND_NON_ZERO or #WIND_EVEN_ODD).
initialCapacity int
the inital capacity, in path segments

GeneralPath

public GeneralPath(Shape shape)
Constructs a GeneralPath from an arbitrary shape object. The Shapes PathIterator path and winding rule will be used.

Method details

recycle

public static synchronized void recycle(GeneralPath p)
Returns a GeneralPath to the reusable object pool for GeneralPaths.

Parameters

p GeneralPath
The path to recycle.

createFromPool

public static GeneralPath createFromPool()

Creates a new GeneralPath from an object Pool. This is useful if you need to create a temporary General path that you wish to dispose of after using.

You should return this object back to the pool when you are done using the #recycle(com.codename1.ui.geom.GeneralPath) method.

isConvexPolygon

public static boolean isConvexPolygon(float[] xPoints, float[] yPoints)
Checks to see if the set of points form a convex polygon. if

Parameters

xPoints float[]
The x coordinates of the polygon.
yPoints float[]
The y coordinates of the polygon.

Returns

True if the points comprise a convex polygon.

isConvexPolygon

public static boolean isConvexPolygon(int[] xPoints, int[] yPoints)

isPolygon

public boolean isPolygon()
Checks to see if this path forms a polygon.

Returns

True if the path is a polygon.

getTypesSize

public int getTypesSize()
Returns the number of path commands in this path.

Returns

The number of path commands in this path.

getPointsSize

public int getPointsSize()
Returns the number of points in this path.

Returns

The number of points in this path.

getTypes

public void getTypes(byte[] out)
Returns a copy of the types (aka path commands) in this path.

Parameters

out byte[]
An array to copy the path commands into.

getPoints

public void getPoints(float[] out)
Returns a copy of the points in this path.

Parameters

out float[]
An array to copy the points into.

toString

public String toString()
Returns a string representation of the object. In general, the toString method returns a string that “textually represents” this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + ‘@’ + Integer.toHexString(hashCode())

equals

public boolean equals(Shape shape, Transform t)

getWindingRule

public int getWindingRule()
Returns the path’s current winding rule.

Returns

#WIND_NON_ZERO or #WIND_EVEN_ODD

setWindingRule

public void setWindingRule(int rule)
Sets the path’s winding rule, which controls which areas are considered ‘inside’ or ‘outside’ the path on drawing. Valid rules are #WIND_EVEN_ODD for an even-odd winding rule, or #WIND_NON_ZERO for a non-zero winding rule.

Parameters

rule int
the rule. (#WIND_NON_ZERO or #WIND_EVEN_ODD).

moveTo

public void moveTo(double x, double y)

moveTo

public void moveTo(float x, float y)
Adds a new point to a path.

Parameters

x float
the x-coordinate.
y float
the y-coordinate.

lineTo

public void lineTo(double x, double y)

lineTo

public void lineTo(float x, float y)
Appends a straight line to the current path.

Parameters

x float
x coordinate of the line endpoint.
y float
y coordinate of the line endpoint.

quadTo

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

quadTo

public void quadTo(float x1, float y1, float x2, float y2)
Appends a quadratic Bezier curve to the current path.

Parameters

x1 float
x coordinate of the control point
y1 float
y coordinate of the control point
x2 float
x coordinate of the curve endpoint.
y2 float
y coordinate of the curve endpoint.

curveTo

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

curveTo

public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3)
Appends a cubic Bezier curve to the current path.

Parameters

x1 float
x coordinate of the first control point
y1 float
y coordinate of the first control point
x2 float
x coordinate of the second control point
y2 float
y coordinate of the second control point
x3 float
x coordinate of the curve endpoint.
y3 float
y coordinate of the curve endpoint.

arc

public void arc(float x, float y, float w, float h, float startAngle, float sweepAngle)
Draws an elliptical arc on the path given the provided bounds.

Parameters

x float
Left x coord of bounding rect.
y float
Top y coordof bounding rect.
w float
Width of bounding rect.
h float
Height of bounding rect.
startAngle float
Start angle on ellipse in radians. Counter-clockwise from 3-o’clock.
sweepAngle float
Sweep angle in radians. Counter-clockwise.

arc

public void arc(float x, float y, float w, float h, float startAngle, float sweepAngle, boolean joinPath)
Draws an elliptical arc on the path given the provided bounds.

Parameters

x float
Left x coord of bounding rect.
y float
Top y coordof bounding rect.
w float
Width of bounding rect.
h float
Height of bounding rect.
startAngle float
Start angle on ellipse in radians. Counter-clockwise from 3-o’clock.
sweepAngle float
Sweep angle in radians. Counter-clockwise.
joinPath boolean
If true, then this will join the arc to the existing path with a line.

arc

public void arc(double x, double y, double w, double h, double startAngle, double sweepAngle)
Draws an elliptical arc on the path given the provided bounds.

Parameters

x double
Left x coord of bounding rect.
y double
Top y coordof bounding rect.
w double
Width of bounding rect.
h double
Height of bounding rect.
startAngle double
Start angle on ellipse in radians. Counter-clockwise from 3-o’clock.
sweepAngle double
Sweep angle in radians. Counter-clockwise.

arc

public void arc(double x, double y, double w, double h, double startAngle, double sweepAngle, boolean joinPath)
Draws an elliptical arc on the path given the provided bounds.

Parameters

x double
Left x coord of bounding rect.
y double
Top y coordof bounding rect.
w double
Width of bounding rect.
h double
Height of bounding rect.
startAngle double
Start angle on ellipse in radians. Counter-clockwise from 3-o’clock.
sweepAngle double
Sweep angle in radians. Counter-clockwise.
joinPath boolean
If true then this will join the arc to the existing path with a line.

arcTo

public void arcTo(float cX, float cY, float endX, float endY)

Adds an arc to the path. This method uses an approximation of an arc using a cubic path. It is not a precise arc.

Note: The arc is drawn counter-clockwise around the center point. See double, double, double, boolean) to draw clockwise.

Parameters

cX float
The x-coordinate of the oval center.
cY float
The y-coordinate of the oval center.
endX float
The end X coordinate.
endY float
The end Y coordinate.

arcTo

public void arcTo(float cX, float cY, float endX, float endY, boolean clockwise)
Adds an arc to the path. This method uses an approximation of an arc using a cubic path. It is not a precise arc.

Parameters

cX float
The x-coordinate of the oval center.
cY float
The y-coordinate of the oval center.
endX float
The end X coordinate.
endY float
The end Y coordinate.
clockwise boolean
If true, the arc is drawn clockwise around the center point.

arcTo

public void arcTo(double cX, double cY, double endX, double endY)

Adds an arc to the path. This method uses an approximation of an arc using a cubic path. It is not a precise arc.

Note: The arc is drawn counter-clockwise around the center point. See double, double, double, boolean) to draw clockwise.

Parameters

cX double
The x-coordinate of the oval center.
cY double
The y-coordinate of the oval center.
endX double
The end X coordinate.
endY double
The end Y coordinate.

arcTo

public void arcTo(double cX, double cY, double endX, double endY, boolean clockwise)
Adds an arc to the path. This method uses an approximation of an arc using a cubic path. It is not a precise arc.

Parameters

cX double
The x-coordinate of the oval center.
cY double
The y-coordinate of the oval center.
endX double
The end X coordinate.
endY double
The end Y coordinate.
clockwise boolean
If true, the arc is drawn clockwise around the center point.

closePath

public void closePath()
Closes the current subpath by drawing a line back to the point of the last moveTo, unless the path is already closed.

append

public void append(Shape shape, boolean connect)
Appends the segments of a Shape to the path. If connect is true, the new path segments are connected to the existing one with a line. The winding rule of the Shape is ignored.

Parameters

shape Shape
the shape (null not permitted).
connect boolean
whether to connect the new shape to the existing path.

append

public void append(PathIterator path, boolean connect)
Appends the segments of a PathIterator to this GeneralPath. Optionally, the initial PathIterator#SEG_MOVETO segment of the appended path is changed into a PathIterator#SEG_LINETO segment.

Parameters

path PathIterator
the PathIterator specifying which segments shall be appended (null not permitted).
connect boolean
true for substituting the initial PathIterator#SEG_MOVETO segment by a PathIterator#SEG_LINETO, or false for not performing any substitution. If this GeneralPath is currently empty, connect is assumed to be false, thus leaving the initial PathIterator#SEG_MOVETO unchanged.

getCurrentPoint

public float[] getCurrentPoint()
Returns the current appending point of the path.

Returns

2-element array of the form [x,y] representing x and y coordinate of the current appending point of the path..

getCurrentPoint

public void getCurrentPoint(float[] point)
Sets the coordinates of the given point to the current point in the path.

Parameters

point float[]
Out parameter. Will be filled with the coords of the current point.

reset

public void reset()
Resets the path. All points and segments are destroyed.

getBounds2D

public float[] getBounds2D()
Returns the path’s bounding box, in float precision.

Returns

4-element array of the form [x, y, width, height].

getBounds2D

public void getBounds2D(float[] out)
Sets the 4-element array to the bounding box coordinates of the path. x, y, width, height.

Parameters

out float[]
4-element float[] array.

getBounds

public Rectangle getBounds()
Returns the path’s bounding box.

Returns

The bounding box of the path.

getBounds

public void getBounds(Rectangle out)
Sets the coordinates of the provided rectangle to the bounding box of this path.

isRectangle

public boolean isRectangle()
Checks to see if this path is a rectangle.

Returns

True if this path forms a rectangle. False otherwise.

getPathIterator

public PathIterator getPathIterator()
{Gets an iterator to walk all of the path segments of the shape.}

Returns

A PathIterator that can iterate over the path segments of the shape.

getPathIterator

public PathIterator getPathIterator(Transform m)

{Gets an iterator where all points are transformed by the provided transform.

Note: If com.codename1.ui.Transform#isSupported() is false, then using this iterator will throw a Runtime Exception.}

Returns

A PathIterator where points are transformed by the provided transform.

createTransformedShape

public Shape createTransformedShape(Transform m)

Returns a shape formed by transforming the current shape with the provided transform.

Note: If com.codename1.ui.Transform#isSupported is false, this may throw a RuntimeException.

Parameters

m Transform
The transform to be used to transform the shape.

Returns

The transformed shape.

setPath

public void setPath(GeneralPath p, Transform t)
Sets this path to be identical to the provided path p with the given Transform t applied to it.

Parameters

p GeneralPath
The path to copy.
t Transform
The transform to apply to all points in the path.

setRect

public void setRect(Rectangle r, Transform t)
Sets this path to be a rectangle with the provided bounds, but with the given transform applied to it.

Parameters

r Rectangle
Rectangle to copy.
t Transform
The transform to apply to the points in in r.

setShape

public void setShape(Shape s, Transform t)
Sets this path to be a copy of the provided shape, but with the provided transform applied to it.

Parameters

s Shape
The shape to copy.
t Transform
The transform to apply to all points in the shape.

intersect

public boolean intersect(Rectangle rect)
Sets the current path to the intersection of itself and the provided rectangle.

Parameters

rect Rectangle
The rectangle to intersect with this path.

Returns

True if rect intersects the current path. False otherwise. If there is no intersection, the path will be reset to be empty.

intersect

public boolean intersect(int x, int y, int w, int h)

transform

public void transform(Transform m)
Transforms the current path in place using the given transform.

Parameters

m Transform
The transform to apply to the path.

intersect

public void intersect(Shape shape)

Resets this path to be the intersection of itself with the given shape. Note that only com.codename1.ui.geom.Rectangles are current supported. If you pass any other shape, it will throw a RuntimeException.

Note: If com.codename1.ui.TransformisSupported is false, this will throw a Runtime Exception

Parameters

shape Shape
The shape to intersect with the current shape.

intersection

public Shape intersection(Rectangle rect)
{Returns the shape formed by the intersection of this shape and the provided rectangle.}

Parameters

rect Rectangle
A rectangle with which to form an intersection.

Returns

The shape formed by intersecting the current shape with the provided rectangle.

contains

public boolean contains(float x, float y)
Checks if the given point is contained in the current shape.

Parameters

x float
The x coordinate to check
y float
The y coordinate to check

Returns

True if the point is inside the shape.

contains

public boolean contains(int x, int y)
{Checks if the shape contains the given point.}

Parameters

x int
The x-coordinate of the point to test.
y int
The y-coordinate of the point to test.

Returns

True if (x, y) is inside the shape.