Search in sources :

Example 6 with Shape

use of com.codename1.ui.geom.Shape in project CodenameOne by codenameone.

the class IOSImplementation method createImage.

@Override
public Image createImage(Shape shape, Stroke stroke, int color) {
    NativePathRenderer renderer = renderShape(shape, stroke);
    int[] argb = renderer.toARGB(color);
    int[] bounds = new int[4];
    renderer.getOutputBounds(bounds);
    Image out = Image.createImage(argb, bounds[2] - bounds[0], bounds[3] - bounds[1]);
    renderer.destroy();
    return out;
}
Also used : Image(com.codename1.ui.Image)

Example 7 with Shape

use of com.codename1.ui.geom.Shape in project CodenameOne by codenameone.

the class HTMLComponent method handleImageMapArea.

/**
 * Handles an area definition for an image map
 *
 * @param areaTag The AREA tag
 */
private void handleImageMapArea(HTMLElement areaTag) {
    if (curImageMap != null) {
        String shape = areaTag.getAttributeById(HTMLElement.ATTR_SHAPE);
        boolean supportedShape = false;
        if (shape != null) {
            String hrefStr = areaTag.getAttributeById(HTMLElement.ATTR_HREF);
            if (shape.equalsIgnoreCase("default")) {
                supportedShape = true;
                curImageMap.setDefaultLink(hrefStr);
            } else if ((shape.equalsIgnoreCase("rect")) || (shape.equalsIgnoreCase("circle"))) {
                supportedShape = true;
                String coordsStr = areaTag.getAttributeById(HTMLElement.ATTR_COORDS);
                if ((coordsStr != null) && (hrefStr != null)) {
                    String curValStr = "";
                    int[] coords = new int[4];
                    int curCoord = 0;
                    boolean error = true;
                    try {
                        for (int c = 0; c < coordsStr.length(); c++) {
                            char ch = coordsStr.charAt(c);
                            if (ch != ',') {
                                curValStr += ch;
                            } else {
                                coords[curCoord] = Integer.parseInt(curValStr);
                                curCoord++;
                                curValStr = "";
                            }
                        }
                        if (curValStr.length() > 0) {
                            coords[curCoord] = Integer.parseInt(curValStr);
                            curCoord++;
                        }
                        if (shape.equalsIgnoreCase("rect")) {
                            if (curCoord == 4) {
                                curImageMap.addRectArea(new Rectangle(coords[0], coords[1], coords[2] - coords[0], coords[3] - coords[1]), hrefStr);
                                error = false;
                            }
                        } else if (curCoord == 3) {
                            // circle
                            // coords[2] is the radius, and 0,1 are the center x,y
                            curImageMap.addRectArea(new Rectangle(coords[0] - coords[2], coords[1] - coords[2], coords[2] * 2 + 1, coords[2] * 2 + 1), hrefStr);
                            // Note: The 'circle' SHAPE in AREA tag is implemented as a rectangle to avoid complication of circle pixel collision
                            error = false;
                        }
                    } catch (Exception e) {
                    // Can be number format exception or index out of bounds
                    // do nothing - error will stay true
                    }
                    if (error) {
                        notifyImageMapError("AREA tag 'coords' property value is invalid (should be exactly 3 comma seperated numbers for circle, 4 for rectangle): " + coordsStr, HTMLCallback.ERROR_ATTIBUTE_VALUE_INVALID, HTMLElement.ATTR_COORDS, coordsStr);
                    }
                }
            }
        }
        if (!supportedShape) {
            notifyImageMapError("Unsupported or missing AREA tag 'shape' property: " + shape, HTMLCallback.ERROR_ATTIBUTE_VALUE_INVALID, HTMLElement.ATTR_SHAPE, shape);
        }
    } else {
        notifyImageMapError("AREA tag is defined without a parent MAP tag - ignoring", HTMLCallback.ERROR_INVALID_TAG_HIERARCHY, -1, null);
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 8 with Shape

use of com.codename1.ui.geom.Shape in project CodenameOne by codenameone.

the class Graphics method setClip.

/**
 * Clips the Graphics context to the Shape.
 * <p>This is not supported on all platforms and contexts currently.
 * Use {@link #isShapeClipSupported} to check if the current
 * context supports clipping shapes.</p>
 *
 * <script src="https://gist.github.com/codenameone/65f531adae2e8c22afc8.js"></script>
 * <img src="https://www.codenameone.com/img/blog/shaped-clipping.png" alt="Shaped clipping in action" />
 *
 * @param shape The shape to clip.
 * @see #isShapeClipSupported
 */
public void setClip(Shape shape) {
    if (xTranslate != 0 || yTranslate != 0) {
        GeneralPath p = tmpClipShape();
        p.setShape(shape, translation());
        shape = p;
    }
    impl.setClip(nativeGraphics, shape);
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath)

Example 9 with Shape

use of com.codename1.ui.geom.Shape in project CodenameOne by codenameone.

the class Graphics method drawShape.

// --------------------------------------------------------------------------
// START SHAPE DRAWING STUFF
// --------------------------------------------------------------------------
/**
 * Draws a outline shape inside the specified bounding box.  The bounding box will resize the shape to fit in its dimensions.
 * <p>This is not supported on
 * all platforms and contexts currently.  Use {@link #isShapeSupported} to check if the current
 * context supports drawing shapes.</p>
 *
 * <script src="https://gist.github.com/codenameone/3f2f8cdaabb7780eae6f.js"></script>
 * <img src="https://www.codenameone.com/img/developer-guide/graphics-shape-fill.png" alt="Fill a shape general path" />
 *
 * @param shape The shape to be drawn.
 * @param stroke the stroke to use
 *
 * @see #setStroke
 * @see #isShapeSupported
 */
public void drawShape(Shape shape, Stroke stroke) {
    if (isShapeSupported()) {
        if (xTranslate != 0 || yTranslate != 0) {
            GeneralPath p = tmpClipShape();
            p.setShape(shape, translation());
            shape = p;
        }
        impl.drawShape(nativeGraphics, shape, stroke);
    }
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath)

Example 10 with Shape

use of com.codename1.ui.geom.Shape in project CodenameOne by codenameone.

the class Graphics method fillShape.

/**
 * Fills the given shape using the current alpha and color settings.
 *  <p>This is not supported on
 * all platforms and contexts currently.  Use {@link #isShapeSupported} to check if the current
 * context supports drawing shapes.</p>
 *
 * <script src="https://gist.github.com/codenameone/3f2f8cdaabb7780eae6f.js"></script>
 * <img src="https://www.codenameone.com/img/developer-guide/graphics-shape-fill.png" alt="Fill a shape general path" />
 *
 * @param shape The shape to be filled.
 *
 * @see #isShapeSupported
 */
public void fillShape(Shape shape) {
    if (isShapeSupported()) {
        if (xTranslate != 0 || yTranslate != 0) {
            GeneralPath p = tmpClipShape();
            p.setShape(shape, translation());
            shape = p;
        }
        impl.fillShape(nativeGraphics, shape);
    }
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath)

Aggregations

GeneralPath (com.codename1.ui.geom.GeneralPath)6 Rectangle (com.codename1.ui.geom.Rectangle)5 Transform (com.codename1.ui.Transform)2 GradientDrawable (com.codename1.charts.compat.GradientDrawable)1 XYChart (com.codename1.charts.views.XYChart)1 Image (com.codename1.ui.Image)1 PathIterator (com.codename1.ui.geom.PathIterator)1 Shape (com.codename1.ui.geom.Shape)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1