Search in sources :

Example 1 with GeneralPath

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

the class RoundRectBorder method createShape.

private GeneralPath createShape(int shapeW, int shapeH) {
    GeneralPath gp = new GeneralPath();
    float radius = Display.getInstance().convertToPixels(cornerRadius);
    float x = 0;
    float y = 0;
    float widthF = shapeW;
    float heightF = shapeH;
    if (this.stroke != null && strokeOpacity > 0 && strokeThickness > 0) {
        int strokePx = (int) strokeThickness;
        if (strokeMM) {
            strokePx = Display.getInstance().convertToPixels(strokeThickness);
        }
        widthF -= strokePx;
        heightF -= strokePx;
        x += strokePx / 2;
        y += strokePx / 2;
        if (strokePx % 2 == 1) {
            x += 0.5f;
            y += 0.5f;
        }
    }
    if (topLeft) {
        gp.moveTo(x + radius, y);
    } else {
        gp.moveTo(x, y);
    }
    if (topRight) {
        gp.lineTo(x + widthF - radius, y);
        gp.quadTo(x + widthF, y, x + widthF, y + radius);
    } else {
        gp.lineTo(x + widthF, y);
    }
    if (bottomRight) {
        gp.lineTo(x + widthF, y + heightF - radius);
        gp.quadTo(x + widthF, y + heightF, x + widthF - radius, y + heightF);
    } else {
        gp.lineTo(x + widthF, y + heightF);
    }
    if (bottomLeft) {
        gp.lineTo(x + radius, y + heightF);
        gp.quadTo(x, y + heightF, x, y + heightF - radius);
    } else {
        gp.lineTo(x, y + heightF);
    }
    if (topLeft) {
        gp.lineTo(x, y + radius);
        gp.quadTo(x, y, x + radius, y);
    } else {
        gp.lineTo(x, y);
    }
    gp.closePath();
    return gp;
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath)

Example 2 with GeneralPath

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

the class RoundRectBorder method paintBorderBackground.

@Override
public void paintBorderBackground(Graphics g, final Component c) {
    final int w = c.getWidth();
    final int h = c.getHeight();
    int x = c.getX();
    int y = c.getY();
    if (shadowOpacity == 0) {
        Style s = c.getStyle();
        if (s.getBgImage() == null) {
            byte type = s.getBackgroundType();
            if (type == Style.BACKGROUND_IMAGE_SCALED || type == Style.BACKGROUND_NONE) {
                GeneralPath gp = createShape(w, h);
                byte bgt = c.getStyle().getBgTransparency();
                if (bgt != 0) {
                    int a = g.getAlpha();
                    g.setAlpha(bgt & 0xff);
                    g.setColor(s.getBgColor());
                    g.translate(x, y);
                    g.fillShape(gp);
                    if (this.stroke != null && strokeOpacity > 0 && strokeThickness > 0) {
                        g.setAlpha(strokeOpacity);
                        g.setColor(strokeColor);
                        g.drawShape(gp, this.stroke);
                    }
                    g.translate(-x, -y);
                    g.setAlpha(a);
                }
                if (this.stroke != null && strokeOpacity > 0 && strokeThickness > 0) {
                    int a = g.getAlpha();
                    g.setAlpha(strokeOpacity);
                    g.setColor(strokeColor);
                    g.translate(x, y);
                    g.drawShape(gp, this.stroke);
                    g.translate(-x, -y);
                    g.setAlpha(a);
                }
                return;
            }
        }
    }
    if (w > 0 && h > 0) {
        Image background = (Image) c.getClientProperty(CACHE_KEY + instanceVal);
        if (background != null && background.getWidth() == w && background.getHeight() == h) {
            g.drawImage(background, x, y);
            return;
        }
    } else {
        return;
    }
    Image target = createTargetImage(c, w, h, true);
    g.drawImage(target, x, y);
    c.putClientProperty(CACHE_KEY + instanceVal, target);
    // update the cache with a more refined version and repaint
    Display.getInstance().callSeriallyOnIdle(new Runnable() {

        public void run() {
            if (w == c.getWidth() && h == c.getHeight()) {
                Image target = createTargetImage(c, w, h, false);
                c.putClientProperty(CACHE_KEY + instanceVal, target);
                c.repaint();
            }
        }
    });
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath) Image(com.codename1.ui.Image)

Example 3 with GeneralPath

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

the class CubicLineChart method drawPath.

@Override
protected void drawPath(Canvas canvas, List<Float> points, Paint paint, boolean circular) {
    GeneralPath p = new GeneralPath();
    float x = points.get(0);
    float y = points.get(1);
    p.moveTo(x, y);
    int length = points.size();
    if (circular) {
        length -= 4;
    }
    Point p1 = new Point();
    Point p2 = new Point();
    Point p3 = new Point();
    for (int i = 0; i < length; i += 2) {
        int nextIndex = i + 2 < length ? i + 2 : i;
        int nextNextIndex = i + 4 < length ? i + 4 : nextIndex;
        calc(points, p1, i, nextIndex, mSecondMultiplier);
        p2.setX(points.get(nextIndex));
        p2.setY(points.get(nextIndex + 1));
        calc(points, p3, nextIndex, nextNextIndex, mFirstMultiplier);
        // From last point, approaching x1/y1 and x2/y2 and ends up at x3/y3
        p.curveTo(p1.getX(), p1.getY(), p2.getX(), p2.getY(), p3.getX(), p3.getY());
    }
    mPathMeasure = new PathMeasure(p, false);
    if (circular) {
        for (int i = length; i < length + 4; i += 2) {
            p.lineTo(points.get(i), points.get(i + 1));
        }
        p.lineTo(points.get(0), points.get(1));
    }
    canvas.drawPath(p, paint);
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath) PathMeasure(com.codename1.charts.compat.PathMeasure) Point(com.codename1.charts.models.Point) Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint)

Example 4 with GeneralPath

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

the class PieSegment method getShape.

public Shape getShape(float cX, float cY, float radius) {
    GeneralPath out = new GeneralPath();
    out.moveTo(cX, cY);
    out.lineTo(cX + radius * Math.cos(Math.toRadians(mStartAngle)), cY + radius * Math.sin(Math.toRadians(mStartAngle)));
    out.arcTo(cX, cY, cX + radius * Math.cos(Math.toRadians(mEndAngle)), cY + radius * Math.sin(Math.toRadians(mEndAngle)));
    out.closePath();
    return out;
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath)

Example 5 with GeneralPath

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

the class AbstractChart method drawPath.

/**
 * The graphical representation of a path.
 *
 * @param canvas the canvas to paint to
 * @param points the points that are contained in the path to paint
 * @param paint the paint to be used for painting
 * @param circular if the path ends with the start point
 */
protected void drawPath(Canvas canvas, List<Float> points, Paint paint, boolean circular) {
    GeneralPath path = new GeneralPath();
    int height = canvas.getHeight();
    int width = canvas.getWidth();
    float[] tempDrawPoints;
    if (points.size() < 4) {
        return;
    }
    tempDrawPoints = calculateDrawPoints(points.get(0), points.get(1), points.get(2), points.get(3), height, width);
    path.moveTo(tempDrawPoints[0], tempDrawPoints[1]);
    path.lineTo(tempDrawPoints[2], tempDrawPoints[3]);
    int length = points.size();
    for (int i = 4; i < length; i += 2) {
        if ((points.get(i - 1) < 0 && points.get(i + 1) < 0) || (points.get(i - 1) > height && points.get(i + 1) > height)) {
            continue;
        }
        tempDrawPoints = calculateDrawPoints(points.get(i - 2), points.get(i - 1), points.get(i), points.get(i + 1), height, width);
        if (!circular) {
            path.moveTo(tempDrawPoints[0], tempDrawPoints[1]);
        }
        path.lineTo(tempDrawPoints[2], tempDrawPoints[3]);
    }
    if (circular) {
        path.lineTo(points.get(0), points.get(1));
    }
    canvas.drawPath(path, paint);
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath) Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint)

Aggregations

GeneralPath (com.codename1.ui.geom.GeneralPath)16 Paint (com.codename1.charts.compat.Paint)3 Point (com.codename1.charts.models.Point)3 Image (com.codename1.ui.Image)3 Graphics (com.codename1.ui.Graphics)2 Transform (com.codename1.ui.Transform)2 Rectangle (com.codename1.ui.geom.Rectangle)2 PathMeasure (com.codename1.charts.compat.PathMeasure)1 Component (com.codename1.ui.Component)1 Dialog (com.codename1.ui.Dialog)1 Form (com.codename1.ui.Form)1