examples.geometry.containment.PolygonCubicCurveContainment.java Source code

Java tutorial

Introduction

Here is the source code for examples.geometry.containment.PolygonCubicCurveContainment.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.CubicCurve;
import org.eclipse.gef4.geometry.planar.IGeometry;
import org.eclipse.gef4.geometry.planar.Path;
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 PolygonCubicCurveContainment extends AbstractPolygonContainmentExample {

    public PolygonCubicCurveContainment(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() {
                addControlPoint(new Point(200, 100));
                addControlPoint(new Point(190, 310));
                addControlPoint(new Point(410, 90));
                addControlPoint(new Point(400, 300));
            }

            @Override
            public CubicCurve createGeometry() {
                return new CubicCurve(getControlPoints());
            }

            @Override
            public void drawShape() {
                CubicCurve c = createGeometry();
                Path path = c.toPath();
                CanvasDrawer.strokePath(path, canvas.getContext2d());
                //            gc.drawPath(new org.eclipse.swt.graphics.Path(Display
                //                  .getCurrent(), Geometry2SWT.toSWTPathData(c.toPath())));
            }

            @Override
            public void fillShape(CssColor color) {
                //            int lineWidth = gc.getLineWidth();
                //            Color fg = gc.getForeground();
                Context2d context2d = canvas.getContext2d();
                FillStrokeStyle fillStyle = context2d.getFillStyle();
                context2d.setLineWidth(3);
                context2d.setStrokeStyle(color.value());

                //            gc.setLineWidth(3);
                //            gc.setForeground(gc.getBackground());
                drawShape();

                context2d.setLineWidth(1);
                context2d.setStrokeStyle(fillStyle);

                //            gc.setForeground(fg);
                //            gc.setLineWidth(lineWidth);
            }
        };
    }

}