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:examples.geometry.demos.ConvexHullExample.java

License:Open Source License

protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
    ControllableShape shape = new ControllableShape(canvas, eventBus) {
        {/* w  w w . j  av a 2  s.c om*/
            // These are the points which are displayed on the screen.
            // We will compute their convex hull later.
            addControlPoints(new Point(100, 100), new Point(150, 400), new Point(200, 300), new Point(250, 150),
                    new Point(300, 250), new Point(350, 200), new Point(400, 350));
        }

        @Override
        public Polygon getShape() {
            // Compute the convex hull of the defined point list.
            // We return the convex hull as a Polygon.
            return new Polygon(Point.getConvexHull(getPoints()));
        }

        @Override
        public void onDraw(Canvas canvas) {
            Context2d context = canvas.getContext2d();
            // This is the code to display the computed convex hull.

            // Compute the convex hull.
            Polygon convexHull = getShape();
            for (Line s : convexHull.getOutlineSegments()) {
                Point p1 = s.getP1();
                Point p2 = s.getP2();
                context.moveTo(p1.x, p1.y);
                context.lineTo(p2.x, p2.y);
                context.stroke();
            }

            // Display the convex hull as an SWT Path.
            //            gc.drawPath(new org.eclipse.swt.graphics.Path(Display
            //                  .getCurrent(), Geometry2SWT.toSWTPathData(convexHull
            //                  .toPath())));
        }
    };

    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 a  2s . c o  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.RegionOutlineExample.java

License:Open Source License

@Override
protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
    return new ControllableShape[] { new ControllableShape(canvas, eventBus) {
        {/* w  ww  . ja  va 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  w w  .  j  a  v  a 2s.co 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);
        }
    } };
}

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

License:Open Source License

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

        @Override
        public Polygon getShape() {
            Polygon p = new Polygon(getPoints());
            return p;
        }

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

            Polygon[] triangulation;
            try {
                triangulation = p.getTriangulation();
            } catch (NonSimplePolygonException x) {
                triangulation = new Polygon[] { p };
            }

            for (Polygon triangle : triangulation) {
                context.beginPath();
                context.setStrokeStyle("red");

                for (Line s : triangle.getOutlineSegments()) {
                    Point p1 = s.getP1();
                    Point p2 = s.getP2();
                    context.moveTo(p1.x, p1.y);
                    context.lineTo(p2.x, p2.y);
                    context.stroke();
                }
                context.closePath();
            }

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

            context.beginPath();
            for (Line s : p.getOutlineSegments()) {
                Point p1 = s.getP1();
                Point p2 = s.getP2();
                context.moveTo(p1.x, p1.y);
                context.lineTo(p2.x, p2.y);
                context.stroke();
            }
            context.closePath();
            context.setLineWidth(lineWidth);
        }
    } };
}

From source file:forplay.html.HtmlPath.java

License:Apache License

void replay(Context2d ctx) {
    ctx.beginPath();//from  w  w  w  . j  a  v  a2s. co  m

    int len = list.length(), i = 0;
    double x = 0, y = 0;
    while (i < len) {
        switch ((int) list.get(i++)) {
        case CMD_MOVE: {
            x = list.get(i++);
            y = list.get(i++);
            ctx.moveTo(x, y);
            break;
        }
        case CMD_LINE: {
            x = list.get(i++);
            y = list.get(i++);
            ctx.lineTo(x, y);
            break;
        }
        case CMD_QUAD: {
            double cpx = list.get(i++);
            double cpy = list.get(i++);
            x = list.get(i++);
            y = list.get(i++);
            ctx.quadraticCurveTo(cpx, cpy, x, y);
            break;
        }
        case CMD_ARC: {
            double curX = x, curY = 0;
            double radius = list.get(i++);
            x = list.get(i++);
            y = list.get(i++);
            ctx.arcTo(curX, curY, x, y, radius);
            break;
        }
        case CMD_CLOSE: {
            ctx.closePath();
            break;
        }

        default:
            throw new AssertionError("Corrupt command list");
        }
    }
}

From source file:gwt.g2d.client.graphics.DashedLineRenderer.java

License:Apache License

/**
 * Draw a dashed line from (fromX, fromY) to (toX, toY).
 * /* ww  w. j a  v a  2 s.c  o m*/
 * @param context
 * @param fromX x-coordinate of the starting point
 * @param fromY y-coordinate of the starting point
 * @param toX x-coordinate of the ending point
 * @param toY y-coordinate of the ending point
 * @param dashLength length of the dash
 * @param gapLength length of the gap in between dashes
 */
public static void drawDashedLine(Context2d context, double fromX, double fromY, double toX, double toY,
        double dashLength, double gapLength) {
    DashedLineHelper checkX = GreaterThanHelper.instance;
    DashedLineHelper checkY = GreaterThanHelper.instance;

    if (fromY - toY > 0) {
        checkY = LessThanHelper.instance;
    }
    if (fromX - toX > 0) {
        checkX = LessThanHelper.instance;
    }

    context.moveTo(fromX, fromY);
    double offsetX = fromX;
    double offsetY = fromY;
    boolean dash = true;
    double ang = Math.atan2(toY - fromY, toX - fromX);
    while (!(checkX.isThereYet(offsetX, toX) && checkY.isThereYet(offsetY, toY))) {
        double len = (dash) ? dashLength : gapLength;

        offsetX = checkX.getCap(toX, offsetX + (Math.cos(ang) * len));
        offsetY = checkY.getCap(toY, offsetY + (Math.sin(ang) * len));

        if (dash) {
            context.lineTo(offsetX, offsetY);
        } else {
            context.moveTo(offsetX, offsetY);
        }
        dash = !dash;
    }
}

From source file:gwt.g2d.client.graphics.visitor.LineSegmentVisitor.java

License:Apache License

@Override
public void visit(Surface surface) {
    Context2d context = surface.getContext();
    context.moveTo(fromX, fromY);
    context.lineTo(toX, toY);
}

From source file:gwt.g2d.client.graphics.visitor.RectangleVisitor.java

License:Apache License

@Override
public void visit(Surface surface) {
    Context2d context = surface.getContext();
    context.moveTo(x, y);/*  w ww.  j  ava 2  s  .c  o m*/
    context.lineTo(x + width, y);
    context.lineTo(x + width, y + height);
    context.lineTo(x, y + height);
    context.lineTo(x, y);
}

From source file:net.exclaimindustries.paste.braket.client.ui.BracketCell.java

License:BSD License

private void drawLeftBracket() {
    Context2d con = initBracketCanvas(mCanvas);

    con.beginPath();//from  ww w  .  j  a  v  a  2 s  .c  o m

    int width = mCanvas.getOffsetWidth();
    int height = mCanvas.getOffsetHeight();

    if (!mTerminus) {
        con.moveTo(0, height / 4.0);
        con.lineTo(width / 2.0, height / 4.0);
        con.lineTo(width / 2.0, (3.0 * height) / 4.0);
        con.lineTo(0, (3.0 * height) / 4);
        con.stroke();
    }

    con.moveTo(width / 2.0, height / 2.0);
    con.lineTo(width, height / 2.0);
    con.stroke();
}