Example usage for com.google.gwt.canvas.dom.client Context2d beginPath

List of usage examples for com.google.gwt.canvas.dom.client Context2d beginPath

Introduction

In this page you can find the example usage for com.google.gwt.canvas.dom.client Context2d beginPath.

Prototype

public final native void beginPath() ;

Source Link

Document

Begins a new path.

Usage

From source file:edu.umb.jsPedigrees.client.Pelican.PelicanLines.java

License:Open Source License

public static Canvas drawLines(AbsolutePanel panel) {

    Canvas canvas = Canvas.createIfSupported();
    Context2d ctx = canvas.getContext2d();

    canvas.setCoordinateSpaceHeight(panel.getOffsetHeight());
    canvas.setCoordinateSpaceWidth(panel.getOffsetWidth());

    ctx.setStrokeStyle(CssColor.make("0,0,0"));
    ctx.setLineWidth(1.0f);//from   www .  j a v a2 s .  co m
    ctx.setFont("12px sans-serif");

    int fontHeight = 15;
    int fontAscent = 15;

    int dropSize = Math.max(2, Math.min(PelicanPerson.symbolSize / 2,
            3 * (PelicanPerson.ySpace - PelicanPerson.symbolSize - fontHeight) / 4));
    for (int i = 0; i < panel.getWidgetCount(); i++)
        if (panel.getWidget(i) instanceof PelicanPerson) {
            // draw a line from this person to its parents
            PelicanPerson person = (PelicanPerson) panel.getWidget(i);
            if (person.father != null && person.mother != null) {
                //System.out.println("HERE "+String.valueOf(i));
                // find the mother and father
                PelicanPerson father = person.father;
                PelicanPerson mother = person.mother;
                if (father != null && mother != null) {
                    // line between parents
                    int fatherX = panel.getWidgetLeft(father)
                            + ((panel.getWidgetLeft(father) < panel.getWidgetLeft(mother))
                                    ? PelicanPerson.symbolSize
                                    : 0);
                    int motherX = panel.getWidgetLeft(mother)
                            + ((panel.getWidgetLeft(mother) < panel.getWidgetLeft(father))
                                    ? PelicanPerson.symbolSize
                                    : 0);
                    int fatherY = panel.getWidgetTop(father) + PelicanPerson.symbolSize / 2;
                    int motherY = panel.getWidgetTop(mother) + PelicanPerson.symbolSize / 2;
                    int leftX = fatherX;
                    int leftY = fatherY;
                    int rightX = motherX;
                    int rightY = motherY;
                    if (motherX < fatherX) {
                        leftX = motherX;
                        leftY = motherY;
                        rightX = fatherX;
                        rightY = fatherY;
                    }
                    int gap = PelicanPerson.xSpace - PelicanPerson.symbolSize;
                    // see if any subjects lie between the father and mother
                    if (!adjacent(panel, father, mother) && father.generation == mother.generation) {
                        // draw lines which avoid other symbols

                        // g2.drawLine(leftX,leftY,leftX+gap/4,leftY);
                        ctx.beginPath();
                        ctx.moveTo(leftX, leftY);
                        ctx.lineTo(leftX + gap / 4, leftY);
                        ctx.closePath();
                        ctx.stroke();

                        // g2.drawLine(rightX,rightY,rightX-gap/2,rightY);
                        ctx.beginPath();
                        ctx.moveTo(rightX, rightY);
                        ctx.lineTo(rightX - gap / 2, rightY);
                        ctx.closePath();
                        ctx.stroke();

                        leftX += gap / 4;
                        rightX -= gap / 2;

                        // g2.drawLine(leftX,leftY,leftX,leftY-(PelicanPerson.symbolSize+dropSize)/2);
                        ctx.beginPath();
                        ctx.moveTo(leftX, leftY);
                        ctx.lineTo(leftX, leftY - (PelicanPerson.symbolSize + dropSize) / 2);
                        ctx.closePath();
                        ctx.stroke();

                        // g2.drawLine(rightX,rightY,rightX,rightY-(PelicanPerson.symbolSize+dropSize)/2);
                        ctx.beginPath();
                        ctx.moveTo(rightX, rightY);
                        ctx.lineTo(rightX, rightY - (PelicanPerson.symbolSize + dropSize) / 2);
                        ctx.closePath();
                        ctx.stroke();

                        leftY -= (PelicanPerson.symbolSize + dropSize) / 2;
                        rightY -= (PelicanPerson.symbolSize + dropSize) / 2;
                    }

                    // g2.drawLine(leftX,leftY,rightX,rightY);
                    ctx.beginPath();
                    ctx.moveTo(leftX, leftY);
                    ctx.lineTo(rightX, rightY);
                    ctx.closePath();
                    ctx.stroke();

                    // line up from child
                    // g2.drawLine(person.getX()+PelicanPerson.symbolSize/2,person.getY(),person.getX()+PelicanPerson.symbolSize/2,person.getY()-dropSize);
                    ctx.beginPath();
                    ctx.moveTo(panel.getWidgetLeft(person) + PelicanPerson.symbolSize / 2,
                            panel.getWidgetTop(person));
                    ctx.lineTo(panel.getWidgetLeft(person) + PelicanPerson.symbolSize / 2,
                            panel.getWidgetTop(person) - dropSize);
                    ctx.closePath();
                    ctx.stroke();

                    // line across from child
                    // try to attach to an orphan parent
                    int parentX = fatherX;
                    if (father.isOrphan() || mother.isOrphan()) {
                        parentX = Math.max(fatherX, motherX) - gap / 2;
                    } else {
                        // if no orphan parents, go straight up from
                        // middle laid out sib
                        int nsib = 0;
                        for (int j = 0; j < panel.getWidgetCount(); j++)
                            if (panel.getWidget(j) instanceof PelicanPerson) {
                                PelicanPerson sib = (PelicanPerson) panel.getWidget(j);
                                if (areSibs(person, sib))
                                    nsib++;
                            }
                        int sibs = 0;
                        for (int j = 0; j < panel.getWidgetCount() && sibs <= nsib / 2; j++)
                            if (panel.getWidget(j) instanceof PelicanPerson) {
                                PelicanPerson sib = (PelicanPerson) panel.getWidget(j);
                                if (areSibs(person, sib))
                                    sibs++;
                                parentX = panel.getWidgetLeft(sib) + PelicanPerson.symbolSize / 2;
                            }
                        if (nsib > 1 && nsib % 2 == 0)
                            parentX -= PelicanPerson.xSpace / 2;
                        if (parentX <= leftX)
                            parentX = leftX + PelicanPerson.symbolSize / 2;
                        if (parentX >= rightX)
                            parentX = rightX - PelicanPerson.symbolSize / 2;
                    }

                    // g2.drawLine(person.getX()+PelicanPerson.symbolSize/2,person.getY()-dropSize,parentX,person.getY()-dropSize);
                    ctx.beginPath();
                    ctx.moveTo(panel.getWidgetLeft(person) + PelicanPerson.symbolSize / 2,
                            panel.getWidgetTop(person) - dropSize);
                    ctx.lineTo(parentX, panel.getWidgetTop(person) - dropSize);
                    ctx.closePath();
                    ctx.stroke();

                    // line up to parents
                    // Draw a vertical line up to the line joining the parents
                    // if this happens to be not between the parents,
                    // change it to a line to the midpoint between the parents
                    int parentY = (rightX != leftX)
                            ? leftY + (rightY - leftY) * (parentX - leftX) / (rightX - leftX)
                            : (leftY + rightY) / 2;
                    if (rightX == leftX || parentY > Math.max(leftY, rightY)
                            || parentY < Math.min(leftY, rightY)) {
                        // g2.drawLine(parentX,person.getY()-dropSize, (leftX+rightX)/2,(leftY+rightY)/2);
                        ctx.beginPath();
                        ctx.moveTo(parentX, panel.getWidgetTop(person) - dropSize);
                        ctx.lineTo((leftX + rightX) / 2, (leftY + rightY) / 2);
                        ctx.closePath();
                        ctx.stroke();

                    } else {
                        // g2.drawLine(parentX,person.getY()-dropSize,parentX,parentY);
                        ctx.beginPath();
                        ctx.moveTo(parentX, panel.getWidgetTop(person) - dropSize);
                        ctx.lineTo(parentX, parentY);
                        ctx.closePath();
                        ctx.stroke();

                    }
                }
            }

            // write out id
            int verticalPosn = panel.getWidgetTop(person) + PelicanPerson.symbolSize + fontAscent;
            String idString = String.valueOf(person.id);
            int fontWidth = (int) ctx.measureText(idString).getWidth();
            // g2.drawString(idString, 
            //      person.getX() + PelicanPerson.symbolSize/2 - fontWidth/2,
            //      verticalPosn);
            ctx.fillText(idString, panel.getWidgetLeft(person) + PelicanPerson.symbolSize / 2 - fontWidth / 2,
                    verticalPosn);
            verticalPosn += fontAscent;
        }
    return canvas;
}

From source file:edu.umb.jsPedigrees.client.Pelican.PelicanPerson.java

License:Open Source License

public void drawSymbol() {

    Context2d ctx = canvas.getContext2d();

    // clear old symbol
    ctx.clearRect(0, 0, symbolSize + 1, symbolSize + 1);

    ctx.setStrokeStyle(CssColor.make("0,0,0"));
    ctx.setLineWidth(1.0f);/* w  w w.  java 2 s .c o m*/

    if (sex == male) {
        ctx.strokeRect(0, 0, symbolSize, symbolSize);
        if (affection == affected) {
            ctx.fillRect(0, 0, symbolSize, symbolSize);
        }
    }

    if (sex == female) {
        // g2.drawArc(0,0,symbolSize,symbolSize,0,360);
        ctx.beginPath();
        ctx.arc(symbolSize / 2, symbolSize / 2, (symbolSize / 2) - 1, 0, 360);

        if (affection == affected) {
            ctx.fill();
        } else {
            ctx.stroke();
        }
    }
}

From source file:examples.geometry.AbstractExample.java

License:Open Source License

@Override
public void draw() {
    Context2d context = canvas.getContext2d();
    // reset//  w  w  w.  j a  va2  s.c o  m
    context.clearRect(0, 0, canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight());
    context.setFillStyle("black");
    context.setStrokeStyle("black");
    context.setLineWidth(1);

    for (ControllableShape shape : getControllableShapes()) {
        //         e.gc.setForeground(canvas.getDisplay().getSystemColor(shape.shapeColor));
        //         e.gc.setBackground(canvas.getDisplay().getSystemColor(shape.shapeColor));

        shape.onDraw(canvas);
    }

    for (ControllableShape shape : getControllableShapes()) {
        if (shape.isActive()) {
            FillStrokeStyle fillStyle = context.getFillStyle();

            for (ControlPoint cp : shape.controlPoints) {
                context.beginPath();
                context.arc(cp.getX(), cp.getY(), shape.controlRadius, 0, 180);
                context.setFillStyle(shape.controlColor);
                context.fill();
                context.closePath();
            }
            context.setFillStyle(fillStyle);
        }
    }
}

From source file:examples.geometry.containment.AbstractPolygonContainmentExample.java

License:Open Source License

@Override
protected AbstractControllableShape createControllableShape1(final Canvas canvas) {
    return new AbstractControllableShape(canvas) {
        @Override//  w  w  w  .j a  v  a  2 s .  com
        public void createControlPoints() {
            // no control points => user cannot change it
        }

        @Override
        public Polygon createGeometry() {
            double w = canvas.getCoordinateSpaceWidth(), wg = w / 6, h = canvas.getCoordinateSpaceHeight(),
                    hg = h / 6;

            return new Polygon(new Point[] { new Point(wg, hg), new Point(w - wg, h - hg),
                    new Point(wg, h - hg), new Point(w - wg, hg) });
        }

        @Override
        public void drawShape() {
            Context2d context2d = canvas.getContext2d();
            Polygon polygon = createGeometry();
            context2d.setStrokeStyle("black");

            for (Line segment : polygon.getOutlineSegments()) {
                context2d.beginPath();
                context2d.moveTo(segment.getX1(), segment.getY1());
                context2d.lineTo(segment.getX2(), segment.getY2());
                context2d.stroke();
            }
        }
    };
}

From source file:examples.geometry.containment.PolygonLineContainment.java

License:Open Source License

@Override
protected AbstractControllableShape createControllableShape2(final Canvas canvas) {
    return new AbstractControllableShape(canvas) {
        @Override//from   w  ww.java2 s . co  m
        public void createControlPoints() {
            addControlPoint(new Point(100, 100));
            addControlPoint(new Point(300, 300));
        }

        @Override
        public Line createGeometry() {
            Point[] points = getControlPoints();
            return new Line(points[0], points[1]);
        }

        @Override
        public void drawShape() {
            Line line = createGeometry();
            Context2d c = canvas.getContext2d();
            c.beginPath();
            c.moveTo(line.getX1(), line.getY1());
            c.lineTo(line.getX2(), line.getY2());
            c.closePath();
            c.stroke();
        }

        @Override
        public void fillShape(CssColor color) {
            Context2d context2d = canvas.getContext2d();
            FillStrokeStyle style = context2d.getFillStyle();
            context2d.setLineWidth(3);
            context2d.setStrokeStyle(color.value());

            drawShape();

            context2d.setLineWidth(1);
            //            context2d.setStrokeStyle(CssColor.make(0, 0, 0).value());
            context2d.setStrokeStyle(style);
        }
    };
}

From source file:examples.geometry.demos.BezierApproximationExample.java

License:Open Source License

@Override
protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
    ControllableShape shape = new ControllableShape(canvas, eventBus) {
        @Override//from ww  w.  j  av  a  2  s  .co m
        public BezierCurve getShape() {
            return new BezierCurve(getPoints());
        }

        @Override
        public void onDraw(Canvas canvas) {
            Context2d context = canvas.getContext2d();

            // Construct the Bezier curve.
            BezierCurve curve = getShape();
            CanvasDrawer.strokePath(curve.toPath(), context);

            // Display the connection line of its control points.
            context.beginPath();
            for (Point p : curve.getPoints()) {
                context.lineTo(p.x, p.y);
                context.stroke();
            }
        }
    };
    shape.addControlPoints(new Point(100, 200), new Point(150, 250), new Point(200, 150), new Point(250, 250),
            new Point(300, 150), new Point(350, 250), new Point(400, 200));

    return new ControllableShape[] { shape };
}

From source file:examples.geometry.demos.CubicCurveDeCasteljauExample.java

License:Open Source License

@Override
protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
    return new ControllableShape[] { new ControllableShape(canvas, eventBus) {
        {//from  w  w w  .j  a  v  a2  s  .co m
            /*
             * These are the control points used to construct the CubicCurve
             * later.
             */
            addControlPoints(new Point(100, 200), new Point(200, 100), new Point(300, 300),
                    new Point(400, 200));
        }

        @Override
        public CubicCurve getShape() {
            /*
             * Constructs the CubicCurve of the defined control points.
             */
            return new CubicCurve(getPoints());
        }

        @Override
        public void onDraw(Canvas canvas) {
            Context2d context = canvas.getContext2d();
            /*
             * Draws the CubicCurve and the de Casteljau construction for
             * the current parameter value.
             */

            // Construct the CubicCurve from the defined control points.
            CubicCurve curve = getShape();
            CanvasDrawer.strokePath(curve.toPath(), context);

            /*
             * Retrieve control points to compute the linear interpolations
             * of the de Casteljau algorithm.
             */
            Point[] points = getPoints();

            /*
             * Define the colors for the intermediate lines. We have three
             * stages and therefore three different colors for a cubic
             * Bezier curve. This is the case, because the de Casteljau
             * algorithm reduces the number of control points in each
             * iteration until it reaches the actual point on the curve.
             */
            String[] colors = new String[] { "green", "blue", "red" };

            for (int ci = 0; ci < colors.length; ci++) {
                for (int i = 0; i < 3 - ci; i++) {
                    context.beginPath();
                    context.moveTo(points[i].x, points[i].y);
                    context.lineTo(points[i + 1].x, points[i + 1].y);
                    context.setStrokeStyle(colors[ci]);
                    context.stroke();
                    context.closePath();

                    // interpolate point for the next iteration
                    points[i] = new Line(points[i], points[i + 1]).get(parameterValue);

                    // draw point                  
                    context.beginPath();
                    context.arc(points[i].x, points[i].y, 2, 0, 180);
                    context.setStrokeStyle("black");
                    context.stroke();
                    context.closePath();
                }
            }
        }
    } };
}

From source file:examples.geometry.demos.RegionClippingExample.java

License:Open Source License

@Override
protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
    return new ControllableShape[] { new ControllableShape(canvas, eventBus) {
        {/*from  w  ww.  j av a2s.c  o m*/
            addControlPoints(new Point(100, 100), new Point(200, 200));
            addControlPoints(new Point(150, 150), new Point(250, 250));
        }

        @Override
        public Region getShape() {
            Point[] cp = getPoints();
            Region region = new Region(new Rectangle(cp[0], cp[1]), new Rectangle(cp[2], cp[3]));
            return region;
        }

        @Override
        public void onDraw(Canvas canvas) {
            Context2d context = canvas.getContext2d();
            Region region = getShape();

            context.save();
            context.beginPath();
            Rectangle rr = region.getBounds();
            context.rect(rr.getX(), rr.getY(), rr.getWidth(), rr.getHeight());
            //            context.fill();
            context.clip();

            for (int y = 0; y < 800; y += 20) {
                context.fillText(
                        "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
                        20, y);
            }

            context.restore();

            context.setFillStyle("blue");
            context.setGlobalAlpha(0.5);
            context.beginPath();
            for (Rectangle r : region.getShapes()) {
                context.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
            }
            context.closePath();
            context.setFillStyle("black");
            context.setGlobalAlpha(1);
        }
    } };
}

From source file:examples.geometry.demos.RegionOutlineExample.java

License:Open Source License

@Override
protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
    return new ControllableShape[] { new ControllableShape(canvas, eventBus) {
        {/*w  ww .  java  2 s.c o  m*/
            addControlPoints(new Point(100, 50), new Point(300, 100));
            addControlPoints(new Point(250, 200), new Point(350, 330));
            addControlPoints(new Point(100, 200), new Point(190, 325));
            addControlPoints(new Point(150, 300), new Point(280, 380));
        }

        @Override
        public Region getShape() {
            Point[] cp = getPoints();

            Rectangle[] rectangles = new Rectangle[cp.length / 2];
            for (int i = 0; i < rectangles.length; i++) {
                rectangles[i] = new Rectangle(cp[2 * i], cp[2 * i + 1]);
            }

            return new Region(rectangles);
        }

        @Override
        public void onDraw(Canvas canvas) {
            Context2d context = canvas.getContext2d();
            Region region = getShape();

            context.setFillStyle("rgba(0, 0, 255, 0.5)");
            context.setGlobalAlpha(0.5);

            context.beginPath();
            for (Rectangle r : region.getShapes()) {
                context.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
            }
            context.closePath();

            //            gc.setAlpha(255);
            context.setFillStyle("rgba(255, 255, 255, 1)");
            context.setGlobalAlpha(1);
            // gc.setForeground(Display.getCurrent().getSystemColor(
            // SWT.COLOR_RED));
            // for (Rectangle r : region.getShapes()) {
            // gc.drawRectangle(r.toSWTRectangle());
            // }
            //            gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
            context.beginPath();
            for (Line l : region.getOutlineSegments()) {
                Point p1 = l.getP1();
                Point p2 = l.getP2();
                context.moveTo(p1.x, p1.y);
                context.lineTo(p2.x, p2.y);
                context.stroke();
            }
            context.closePath();
        }
    } };
}

From source file:examples.geometry.demos.RingOutlineExample.java

License:Open Source License

@Override
protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
    return new ControllableShape[] { new ControllableShape(canvas, eventBus) {
        {//from w ww  .  j a  v  a2 s  .c o m
            addControlPoints(new Point(100, 100), new Point(400, 100), new Point(400, 200));
            addControlPoints(new Point(400, 100), new Point(400, 400), new Point(300, 400));
            addControlPoints(new Point(400, 400), new Point(100, 400), new Point(100, 300));
            addControlPoints(new Point(100, 400), new Point(100, 100), new Point(200, 100));
        }

        @Override
        public Ring getShape() {
            Point[] cp = getPoints();

            Polygon[] polygons = new Polygon[cp.length / 3];
            for (int i = 0; i < polygons.length; i++) {
                polygons[i] = new Polygon(cp[3 * i], cp[3 * i + 1], cp[3 * i + 2]);
            }

            return new Ring(polygons);
        }

        @Override
        public void onDraw(Canvas canvas) {
            Context2d context = canvas.getContext2d();
            Ring ring = getShape();

            context.setStrokeStyle("black");
            double lineWidth = context.getLineWidth();
            context.setLineWidth(1);

            for (Polyline outline : ring.getOutlines()) {
                context.beginPath();
                for (Line l : outline.getCurves()) {
                    Point p1 = l.getP1();
                    Point p2 = l.getP2();

                    context.moveTo(p1.x, p1.y);
                    context.lineTo(p2.x, p2.y);
                    context.setLineWidth(lineWidth + 1);
                    context.stroke();
                }
                context.closePath();
            }

            context.setLineWidth(lineWidth);
        }
    } };
}