examples.geometry.containment.AbstractPolygonContainmentExample.java Source code

Java tutorial

Introduction

Here is the source code for examples.geometry.containment.AbstractPolygonContainmentExample.java

Source

/*******************************************************************************
 * Copyright (c) 2011 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.containment;

import org.eclipse.gef4.geometry.planar.Line;
import org.eclipse.gef4.geometry.planar.Point;
import org.eclipse.gef4.geometry.planar.Polygon;

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

public abstract class AbstractPolygonContainmentExample extends AbstractContainmentExample {

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

    @Override
    protected AbstractControllableShape createControllableShape1(final Canvas canvas) {
        return new AbstractControllableShape(canvas) {
            @Override
            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();
                }
            }
        };
    }

}