examples.geometry.demos.CAGExample.java Source code

Java tutorial

Introduction

Here is the source code for examples.geometry.demos.CAGExample.java

Source

/*******************************************************************************
 * Copyright (c) 2012 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.demos;

import org.eclipse.gef4.geometry.planar.Ellipse;
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.event.shared.EventBus;

import examples.geometry.AbstractExample;
import examples.geometry.CanvasDrawer;
import examples.geometry.ControlPoint;
import examples.geometry.ControllableShape;

public class CAGExample extends AbstractExample {

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

    protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
        final ControllableShape csTriangle = new ControllableShape(canvas, eventBus) {
            {
                addControlPoints(new Point(100, 150), new Point(350, 120), new Point(200, 300));
            }

            @Override
            public Polygon getShape() {
                return new Polygon(getPoints());
            }

            @Override
            public void onDraw(Canvas gc) {
                // we draw them later
            }
        };
        final ControllableShape csEllipse = new ControllableShape(canvas, eventBus) {
            {
                addControlPoints(new Point(280, 230), new Point(380, 280));
            }

            @Override
            public Ellipse getShape() {
                Point[] points = getPoints();
                double a = Math.abs(points[0].x - points[1].x);
                double b = Math.abs(points[0].y - points[1].y);
                return new Ellipse(points[0].x - a, points[0].y - b, 2 * a, 2 * b);
            }

            @Override
            public void onDraw(Canvas gc) {
                // we draw them later
            }

            @Override
            public void onMove(int dragPointIndex, double oldX, double oldY) {
                if (dragPointIndex == 0) {
                    double dx = controlPoints.get(0).getX() - oldX;
                    double dy = controlPoints.get(0).getY() - oldY;
                    ControlPoint cp = controlPoints.get(1);
                    cp.setX(cp.getX() + dx);
                    cp.setY(cp.getY() + dy);
                }
            }
        };
        ControllableShape other = new ControllableShape(canvas, eventBus) {
            @Override
            public IGeometry getShape() {
                return null; // does not control a geometry
            }

            @Override
            public void onDraw(Canvas canvas) {
                Context2d context = canvas.getContext2d();

                Path trianglePath = csTriangle.getShape().toPath();
                Path ellipsePath = csEllipse.getShape().toPath();
                Path intersection = Path.intersect(trianglePath, ellipsePath);

                context.setFillStyle("red");
                CanvasDrawer.fillPath(trianglePath, context);

                context.setFillStyle("green");
                CanvasDrawer.fillPath(ellipsePath, context);

                context.setFillStyle("yellow");
                CanvasDrawer.fillPath(intersection, context);
            }
        };

        return new ControllableShape[] { csTriangle, csEllipse, other };
    }

}