examples.geometry.containment.PolygonRectangleContainment.java Source code

Java tutorial

Introduction

Here is the source code for examples.geometry.containment.PolygonRectangleContainment.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.Rectangle;

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 PolygonRectangleContainment extends AbstractPolygonContainmentExample {

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

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

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

    @Override
    protected AbstractControllableShape createControllableShape2(final Canvas canvas) {
        return new AbstractControllableShape(canvas) {
            private final double WIDTH = 50;
            private final double HEIGHT = 75;

            @Override
            public void createControlPoints() {
                addControlPoint(new Point(110, 70));
            }

            @Override
            public Rectangle createGeometry() {
                Point[] points = getControlPoints();
                return new Rectangle(points[0].x - WIDTH / 2, points[0].y - HEIGHT / 2, WIDTH, HEIGHT);
            }

            @Override
            public void drawShape() {
                Rectangle rect = createGeometry();
                Context2d context2d = canvas.getContext2d();
                context2d.rect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
                context2d.stroke();
            }

            @Override
            public void fillShape(CssColor color) {
                Rectangle rect = createGeometry();
                Context2d context2d = canvas.getContext2d();
                FillStrokeStyle style = context2d.getFillStyle();
                context2d.setFillStyle(color.value());

                context2d.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
                context2d.setFillStyle(style);
            }
        };
    }
}