examples.geometry.containment.PolygonEllipseContainment.java Source code

Java tutorial

Introduction

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

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

    @Override
    protected boolean computeContains(IGeometry g1, IGeometry g2) {
        return ((Polygon) g1).contains(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() {
                ControlPoint center = addControlPoint(new Point(300, 300));
                ControlPoint a = addControlPoint(new Point(400, 300));
                ControlPoint b = addControlPoint(new Point(300, 200));
                a.setYLink(center);
                b.setXLink(center);
            }

            @Override
            public Ellipse createGeometry() {
                double a = Math.abs(points.get(0).getPoint().x - points.get(1).getPoint().x);
                double b = Math.abs(points.get(0).getPoint().y - points.get(2).getPoint().y);
                return new Ellipse(points.get(0).getPoint().x - a, points.get(0).getPoint().y - b, 2 * a, 2 * b);
            }

            @Override
            public void drawShape() {
                Ellipse ellipse = createGeometry();
                CanvasDrawer.drawOval(ellipse, canvas.getContext2d());
            }

            @Override
            public void fillShape(CssColor color) {
                Ellipse ellipse = createGeometry();
                Context2d context2d = canvas.getContext2d();
                FillStrokeStyle style = context2d.getFillStyle();

                CanvasDrawer.drawOval(ellipse, context2d);
                context2d.setFillStyle(color.value());
                context2d.fill();

                context2d.setFillStyle(style);
            }
        };
    }
}