examples.geometry.containment.PolygonPolygonContainment.java Source code

Java tutorial

Introduction

Here is the source code for examples.geometry.containment.PolygonPolygonContainment.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 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 PolygonPolygonContainment extends AbstractPolygonContainmentExample {

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

    @Override
    protected boolean computeContains(IGeometry g1, IGeometry g2) {
        return ((Polygon) g1).contains((Polygon) 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(200, 200));
            }

            @Override
            public Polygon createGeometry() {
                Point[] points = getControlPoints();
                Polygon polygon = new Polygon(points[0].x - 30, points[0].y - 30, points[0].x + 80,
                        points[0].y - 20, points[0].x - 20, points[0].y + 40);
                return polygon;
            }

            @Override
            public void drawShape() {
                Polygon polygon = createGeometry();
                Context2d c = canvas.getContext2d();
                CanvasDrawer.strokePath(polygon.toPath(), c);
            }

            @Override
            public void fillShape(CssColor color) {
                Polygon polygon = createGeometry();

                Context2d c = canvas.getContext2d();
                FillStrokeStyle style = c.getFillStyle();

                c.setFillStyle(color.value());
                CanvasDrawer.fillPath(polygon.toPath(), c);

                c.setFillStyle(style);
            }
        };
    }
}