examples.geometry.demos.RingOutlineExample.java Source code

Java tutorial

Introduction

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

Source

/*******************************************************************************
 * Copyright (c) 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.Line;
import org.eclipse.gef4.geometry.planar.Point;
import org.eclipse.gef4.geometry.planar.Polygon;
import org.eclipse.gef4.geometry.planar.Polyline;
import org.eclipse.gef4.geometry.planar.Ring;

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.ControllableShape;

public class RingOutlineExample extends AbstractExample {

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

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

}