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

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

Introduction

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

Prototype

public final native void lineTo(double x, double y) ;

Source Link

Document

Adds a line from the current point to the point (x, y) to the current path.

Usage

From source file:com.kk_electronic.kkportal.debug.modules.UsageGraph.java

License:Open Source License

@Inject
public UsageGraph(CpuUsage model) {
    Context2d context = canvas.getContext2d();
    model.addDisplay(this);
    context.setLineWidth(1);/*from   w ww .  ja va2 s  . c  o m*/
    context.setStrokeStyle("black");
    canvas.getElement().getStyle().setBorderWidth(borderSize, Unit.PX);
    canvas.getElement().getStyle().setBorderStyle(BorderStyle.SOLID);
    canvas.getElement().getStyle().setBorderColor(borderColor);

    context.beginPath();
    context.moveTo(1, 1);
    context.lineTo(1, 50);
    context.lineTo(100, 50);
    context.lineTo(50, 1);
    context.closePath();
    context.stroke();
    timer.scheduleRepeating(100);
}

From source file:com.kk_electronic.kkportal.debug.modules.UsageGraph.java

License:Open Source License

private void drawPath(List<? extends Double> values, double shift) {
    if (values == null || values.isEmpty())
        return;// ww  w  .  ja va 2  s  .  c om
    Context2d context = canvas.getContext2d();
    //      context.setTransform(m11, m12, m21, m22, dx, dy)
    context.clearRect(0, 0, canvas.getOffsetWidth(), canvas.getCoordinateSpaceHeight());
    //      context.clear();
    context.beginPath();
    double pixelpersecond = canvas.getCoordinateSpaceWidth() / magicNumber;
    int o = Math.max(60 - values.size(), 0); // How many missing values
    context.moveTo((o - shift * 2 - 1) * pixelpersecond, canvas.getCoordinateSpaceHeight() * values.get(0));
    for (int i = 1, l = values.size(); i < l; i++) {
        double x = (i + o - shift * 2 - 1) * pixelpersecond;
        double y = canvas.getCoordinateSpaceHeight() * (1 - values.get(i));
        context.lineTo(x, y);
    }
    context.stroke();
}

From source file:com.mecatran.otp.gwt.client.view.ItineraryDetailsWidget.java

License:Open Source License

/**
 * Build the background image for the widget, according to the mode. Draw
 * the mode image and a solid line below it with the route color (if in
 * transit mode) or a dotted line (if in road mode). Set the
 * background-image to the generated image for the given widget.
 *//*from   ww w. j  a v a  2  s  . co  m*/
public static void styleComponentWithMode(final Widget widget, TransportMode mode, String color) {
    PlannerResources resources = PlannerResources.INSTANCE;
    ImageResource baseImage = null;
    boolean road = false;
    switch (mode) {
    case WALK:
        road = true;
        color = "#666666";
        baseImage = resources.modeWalkPng();
        break;
    case BICYCLE:
        road = true;
        color = "#23C30B";
        baseImage = resources.modeBicyclePng();
        break;
    case BICYCLE_RENTAL:
        road = true;
        color = "#23C30B";
        baseImage = resources.modeBikeRentalPng();
        break;
    case CAR:
        road = true;
        color = "#333333";
        baseImage = resources.modeCarPng();
        break;
    default:
    case BUS:
        baseImage = resources.modeBusPng();
        break;
    case TRAM:
        baseImage = resources.modeTramPng();
        break;
    case FERRY:
        baseImage = resources.modeFerryPng();
        break;
    case GONDOLA:
        baseImage = resources.modeGondolaPng();
        break;
    case PLANE:
        baseImage = resources.modePlanePng();
        break;
    case RAIL:
        baseImage = resources.modeRailPng();
        break;
    case SUBWAY:
        baseImage = resources.modeSubwayPng();
        break;
    case TROLLEY:
        baseImage = resources.modeTrolleyPng();
        break;
    }
    final String url = baseImage.getSafeUri().asString();
    final Canvas canvas = Canvas.createIfSupported();
    if (canvas != null) {
        int width = baseImage.getWidth();
        int height = 1000;
        canvas.setCoordinateSpaceWidth(width);
        canvas.setCoordinateSpaceHeight(height);
        final Context2d context = canvas.getContext2d();
        context.setLineCap(LineCap.BUTT);
        if (road) {
            context.setStrokeStyle(CssColor.make(color));
            context.setLineWidth(4);
            for (int y = baseImage.getHeight(); y < 1000; y += 7) {
                context.moveTo(width / 2, y);
                context.lineTo(width / 2, y + 5);
            }
            context.stroke();
        } else {
            context.setStrokeStyle(CssColor.make("#000000"));
            context.setLineWidth(5);
            context.moveTo(width / 2, 0);
            context.lineTo(width / 2, height - 1);
            context.stroke();
            context.setStrokeStyle(CssColor.make(color));
            context.setLineWidth(4);
            context.moveTo(width / 2, 0);
            context.lineTo(width / 2, height - 1);
            context.stroke();
        }
        /*
         * HACK ALERT! Image.onLoad event does not fire up when using
         * internal resources (URL is internal data), but using the image
         * immediately does not work (image does not seems to be ready). We
         * defer the processing of the image rendering to a timer delayed a
         * bit.
         */
        Timer timer = new Timer() {
            @Override
            public void run() {
                Image image = new Image(url);
                ImageElement e = ImageElement.as(image.getElement());
                context.drawImage(e, 0, 0);
                String url2 = canvas.toDataUrl("image/png");
                widget.getElement().getStyle().setBackgroundImage("url('" + url2 + "')");
            }
        };
        timer.schedule(500);
    } else {
        widget.getElement().getStyle().setBackgroundImage("url('" + url + "')");
    }
}

From source file:com.philbeaudoin.quebec.client.scene.Arrow.java

License:Apache License

@Override
public void drawUntransformed(double time, Context2d context) {
    context.beginPath();//from  w w  w .j a va 2 s .  c om
    context.moveTo(from.getX(), from.getY());
    context.bezierCurveTo(p1.getX(), p1.getY(), p2.getX(), p2.getY(), p3.getX(), p3.getY());
    context.lineTo(p4.getX(), p4.getY());
    context.lineTo(to.getX(), to.getY());
    context.lineTo(p5.getX(), p5.getY());
    context.lineTo(p6.getX(), p6.getY());
    context.bezierCurveTo(p2.getX(), p2.getY(), p1.getX(), p1.getY(), from.getX(), from.getY());
    context.setLineWidth(0.0045);
    context.setStrokeStyle("#aaa");
    context.stroke();
    context.setLineWidth(0.001);
    context.setStrokeStyle("#000");
    context.stroke();
    context.fill();
}

From source file:com.philbeaudoin.quebec.client.scene.Callout.java

License:Apache License

@Override
public void drawUntransformed(double time, Context2d context) {
    context.beginPath();//from   ww  w  .  j  ava  2 s .c  o m
    context.moveTo(p1.getX(), p1.getY());
    context.lineTo(to.getX(), to.getY());
    context.lineTo(p3.getX(), p3.getY());
    context.lineTo(p1.getX(), p1.getY());
    context.setLineWidth(0.002);
    context.setStrokeStyle("#000");
    context.stroke();
    context.setFillStyle("#aaa");
    context.fill();
}

From source file:com.sencha.gxt.chart.client.draw.engine.Canvas2d.java

License:sencha.com license

private PathCommand appendPathCommands(Context2d ctx, List<PathCommand> commands, PrecisePoint currentPoint,
        PrecisePoint movePoint, PrecisePoint curvePoint, PrecisePoint quadraticPoint) {
    PathCommand last = null;/*from w w  w.ja v a2  s  .c  o m*/
    for (PathCommand cmd : commands) {
        last = cmd;
        if (cmd instanceof MoveTo) {
            MoveTo move = (MoveTo) cmd;

            quadraticPoint.setX(currentPoint.getX());
            quadraticPoint.setY(currentPoint.getY());
            movePoint.setX(move.getX());
            movePoint.setY(move.getY());
            currentPoint.setX(move.getX());
            currentPoint.setY(move.getY());
            curvePoint.setX(move.getX());
            curvePoint.setY(move.getY());

            ctx.moveTo(move.getX(), move.getY());
        } else if (cmd instanceof LineTo) {
            LineTo line = (LineTo) cmd;

            quadraticPoint.setX(currentPoint.getX());
            quadraticPoint.setY(currentPoint.getY());
            currentPoint.setX(line.getX());
            currentPoint.setY(line.getY());
            curvePoint.setX(line.getX());
            curvePoint.setY(line.getY());

            ctx.lineTo(line.getX(), line.getY());
        } else if (cmd instanceof CurveTo) {
            CurveTo curve = (CurveTo) cmd;

            quadraticPoint.setX(currentPoint.getX());
            quadraticPoint.setY(currentPoint.getY());
            currentPoint.setX(curve.getX());
            currentPoint.setY(curve.getY());
            curvePoint.setX(curve.getX2());
            curvePoint.setY(curve.getY2());

            ctx.bezierCurveTo(curve.getX1(), curve.getY1(), curve.getX2(), curve.getY2(), curve.getX(),
                    curve.getY());
        } else if (cmd instanceof CurveToQuadratic) {
            CurveToQuadratic curve = (CurveToQuadratic) cmd;
            double ax = 2.0 * curve.getX1() / 3.0;
            double ay = 2.0 * curve.getY1() / 3.0;

            quadraticPoint.setX(curve.getX1());
            quadraticPoint.setY(curve.getY1());
            currentPoint.setX(curve.getX() / 3.0 + ax);
            currentPoint.setY(curve.getY() / 3.0 + ay);
            curvePoint.setX(curve.getX1());
            curvePoint.setY(curve.getY1());

            ctx.quadraticCurveTo(curve.getX1(), curve.getY1(), curve.getX(), curve.getY());
        } else if (cmd instanceof ClosePath) {
            quadraticPoint.setX(currentPoint.getX());
            quadraticPoint.setY(currentPoint.getY());

            ctx.closePath();
        } else {
            assert cmd instanceof EllipticalArc || cmd instanceof CurveToQuadraticSmooth
                    || cmd instanceof CurveToSmooth || cmd instanceof LineToHorizontal
                    || cmd instanceof LineToVertical : cmd.getClass() + " is not yet implemented";
            last = appendPathCommands(ctx, cmd.toCurve(currentPoint, movePoint, curvePoint, quadraticPoint),
                    currentPoint, movePoint, curvePoint, quadraticPoint);
            CurveTo curve = (CurveTo) last;
            currentPoint.setX(curve.getX());
            currentPoint.setY(curve.getY());
            curvePoint.setX(curve.getX2());
            curvePoint.setY(curve.getY2());
        }

    }
    return last;
}

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  ww w.j a va 2  s.  c  om*/
    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:examples.geometry.containment.AbstractPolygonContainmentExample.java

License:Open Source License

@Override
protected AbstractControllableShape createControllableShape1(final Canvas canvas) {
    return new AbstractControllableShape(canvas) {
        @Override//from   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 w  w. j  a  v  a  2 s.  c o  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/* w w  w.  j  av  a  2 s  .  c o 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 };
}