examples.geometry.demos.RegionClippingExample.java Source code

Java tutorial

Introduction

Here is the source code for examples.geometry.demos.RegionClippingExample.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.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 RegionClippingExample extends AbstractExample {

    public RegionClippingExample(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, 100), new Point(200, 200));
                addControlPoints(new Point(150, 150), new Point(250, 250));
            }

            @Override
            public Region getShape() {
                Point[] cp = getPoints();
                Region region = new Region(new Rectangle(cp[0], cp[1]), new Rectangle(cp[2], cp[3]));
                return region;
            }

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

                context.save();
                context.beginPath();
                Rectangle rr = region.getBounds();
                context.rect(rr.getX(), rr.getY(), rr.getWidth(), rr.getHeight());
                //            context.fill();
                context.clip();

                for (int y = 0; y < 800; y += 20) {
                    context.fillText(
                            "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
                            20, y);
                }

                context.restore();

                context.setFillStyle("blue");
                context.setGlobalAlpha(0.5);
                context.beginPath();
                for (Rectangle r : region.getShapes()) {
                    context.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
                }
                context.closePath();
                context.setFillStyle("black");
                context.setGlobalAlpha(1);
            }
        } };
    }

}