examples.geometry.demos.BezierApproximationExample.java Source code

Java tutorial

Introduction

Here is the source code for examples.geometry.demos.BezierApproximationExample.java

Source

/*******************************************************************************
 * Copyright (c) 2011, 2012 itemis AG and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Matthias Wienand (itemis AG) - initial API and implementation
 *     
 *******************************************************************************/
package examples.geometry.demos;

import org.eclipse.gef4.geometry.planar.BezierCurve;
import org.eclipse.gef4.geometry.planar.Point;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.event.shared.EventBus;

import examples.geometry.AbstractExample;
import examples.geometry.CanvasDrawer;
import examples.geometry.ControllableShape;

public class BezierApproximationExample extends AbstractExample {

    public BezierApproximationExample(EventBus eventBus, Canvas canvas) {
        super(eventBus, canvas);
    }

    @Override
    protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
        ControllableShape shape = new ControllableShape(canvas, eventBus) {
            @Override
            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 };
    }

}