examples.geometry.containment.PolygonLineContainment.java Source code

Java tutorial

Introduction

Here is the source code for examples.geometry.containment.PolygonLineContainment.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.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.canvas.dom.client.CssColor;
import com.google.gwt.canvas.dom.client.FillStrokeStyle;
import com.google.gwt.event.shared.EventBus;

public class PolygonLineContainment extends AbstractPolygonContainmentExample {

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

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

            @Override
            public Line createGeometry() {
                Point[] points = getControlPoints();
                return new Line(points[0], points[1]);
            }

            @Override
            public void drawShape() {
                Line line = createGeometry();
                Context2d c = canvas.getContext2d();
                c.beginPath();
                c.moveTo(line.getX1(), line.getY1());
                c.lineTo(line.getX2(), line.getY2());
                c.closePath();
                c.stroke();
            }

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

                drawShape();

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