examples.geometry.containment.PolygonPolylineContainment.java Source code

Java tutorial

Introduction

Here is the source code for examples.geometry.containment.PolygonPolylineContainment.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.IGeometry;
import org.eclipse.gef4.geometry.planar.Point;
import org.eclipse.gef4.geometry.planar.Polygon;
import org.eclipse.gef4.geometry.planar.Polyline;

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

import examples.geometry.CanvasDrawer;

public class PolygonPolylineContainment extends AbstractPolygonContainmentExample {

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

    @Override
    protected boolean computeContains(IGeometry g1, IGeometry g2) {
        return ((Polygon) g1).contains((Polyline) g2);
    }

    @Override
    protected boolean computeIntersects(IGeometry g1, IGeometry g2) {
        return ((Polygon) g1).touches(g2);
    }

    @Override
    protected AbstractControllableShape createControllableShape2(final Canvas canvas) {
        return new AbstractControllableShape(canvas) {
            @Override
            public void createControlPoints() {
                addControlPoint(new Point(100, 100));
                addControlPoint(new Point(100, 300));
                addControlPoint(new Point(300, 300));
                addControlPoint(new Point(300, 100));
            }

            @Override
            public Polyline createGeometry() {
                Point[] points = getControlPoints();
                Polyline polyline = new Polyline(points);
                return polyline;
            }

            @Override
            public void drawShape() {
                Polyline polyline = createGeometry();
                CanvasDrawer.strokePath(polyline.toPath(), canvas.getContext2d());
            }

            @Override
            public void fillShape(CssColor color) {
                Context2d context2d = canvas.getContext2d();
                FillStrokeStyle style = context2d.getStrokeStyle();
                context2d.setLineWidth(3);
                context2d.setStrokeStyle(color.value());

                drawShape();

                context2d.setLineWidth(1);
                //            context2d.setStrokeStyle(CssColor.make(0, 0, 0).value());
                context2d.setStrokeStyle(style);
            }
        };
    }
}