examples.geometry.demos.RegionOutlineExample.java Source code

Java tutorial

Introduction

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

Source

/*******************************************************************************
 * Copyright (c) 2011, 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.Line;
import org.eclipse.gef4.geometry.planar.Point;
import org.eclipse.gef4.geometry.planar.Rectangle;
import org.eclipse.gef4.geometry.planar.Region;

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.ControllableShape;

public class RegionOutlineExample extends AbstractExample {

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

    @Override
    protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
        return new ControllableShape[] { new ControllableShape(canvas, eventBus) {
            {
                addControlPoints(new Point(100, 50), new Point(300, 100));
                addControlPoints(new Point(250, 200), new Point(350, 330));
                addControlPoints(new Point(100, 200), new Point(190, 325));
                addControlPoints(new Point(150, 300), new Point(280, 380));
            }

            @Override
            public Region getShape() {
                Point[] cp = getPoints();

                Rectangle[] rectangles = new Rectangle[cp.length / 2];
                for (int i = 0; i < rectangles.length; i++) {
                    rectangles[i] = new Rectangle(cp[2 * i], cp[2 * i + 1]);
                }

                return new Region(rectangles);
            }

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

                context.setFillStyle("rgba(0, 0, 255, 0.5)");
                context.setGlobalAlpha(0.5);

                context.beginPath();
                for (Rectangle r : region.getShapes()) {
                    context.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
                }
                context.closePath();

                //            gc.setAlpha(255);
                context.setFillStyle("rgba(255, 255, 255, 1)");
                context.setGlobalAlpha(1);
                // gc.setForeground(Display.getCurrent().getSystemColor(
                // SWT.COLOR_RED));
                // for (Rectangle r : region.getShapes()) {
                // gc.drawRectangle(r.toSWTRectangle());
                // }
                //            gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
                context.beginPath();
                for (Line l : region.getOutlineSegments()) {
                    Point p1 = l.getP1();
                    Point p2 = l.getP2();
                    context.moveTo(p1.x, p1.y);
                    context.lineTo(p2.x, p2.y);
                    context.stroke();
                }
                context.closePath();
            }
        } };
    }

}