public interface Shape

Known subtypesGeneralPath, Rectangle, Rectangle2D

An interface that can be implemented by any class that wants to be drawable as a shape.

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();

Shape can also be used to clip an area e.g.:

Image duke = null;
try {
    // duke.png is just the default Codename One icon copied into place
    duke = Image.createImage("/duke.png");
} catch(IOException err) {
    Log.e(err);
}
final Image finalDuke = duke;

Form hi = new Form("Shape Clip");

// 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);

Stroke stroke = new Stroke(0.5f, Stroke.CAP_ROUND, Stroke.JOIN_ROUND, 4);
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.setClip(path);
        g.setAntiAliased(true);
        g.drawImage(finalDuke, 0, 0, 50, 100);
        g.setClip(path.getBounds());
        g.drawShape(path, stroke);
        g.translate(-(int) (((float) rect.getX()) / widthRatio), -(int) (((float) rect.getY()) / heightRatio));
        g.resetAffine();
    }
});

hi.show();

Methods

public abstract PathIterator getPathIterator()Gets an iterator to walk all of the path segments of the shape.
public abstract PathIterator getPathIterator(Transform transform)Gets an iterator where all points are transformed by the provided transform.
public abstract Rectangle getBounds()Returns the bounding rectangle for the shape.
public abstract float[] getBounds2D()Gets the bounds of the shape as a 4-element array representing the (x,y,width,height) tuple.
public abstract boolean isRectangle()Checks if this shape is a rectangle.
public abstract boolean contains(int x, int y)Checks if the shape contains the given point.
public abstract Shape intersection(Rectangle rect)Returns the shape formed by the intersection of this shape and the provided rectangle.

Method details

getPathIterator

public abstract 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 abstract PathIterator getPathIterator(Transform transform)

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.

getBounds

public abstract Rectangle getBounds()
Returns the bounding rectangle for the shape. This should be the smallest rectangle such that the all path segments in the shape are contained within it.

Returns

A Rectangle that comprises the bounds of the shape.

getBounds2D

public abstract float[] getBounds2D()
Gets the bounds of the shape as a 4-element array representing the (x,y,width,height) tuple.

Returns

[x, y, width, height] bounds of this shape.

isRectangle

public abstract boolean isRectangle()
Checks if this shape is a rectangle. A Shape is a rectangle if it is a closed quadrilateral composed of two vertical lines and two horizontal lines. If all points have integer coordinates, and this returns true, then getBounds() should return an equivalent rectangle to the shape itself.

Returns

True if shape is a rectangle.

contains

public abstract 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.

intersection

public abstract 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.