Java tutorial
/******************************************************************************* * 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.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.ControllableShape; public class ConvexHullExample extends AbstractExample { public ConvexHullExample(EventBus eventBus, Canvas canvas) { super(eventBus, canvas); } protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) { ControllableShape shape = new ControllableShape(canvas, eventBus) { { // These are the points which are displayed on the screen. // We will compute their convex hull later. addControlPoints(new Point(100, 100), new Point(150, 400), new Point(200, 300), new Point(250, 150), new Point(300, 250), new Point(350, 200), new Point(400, 350)); } @Override public Polygon getShape() { // Compute the convex hull of the defined point list. // We return the convex hull as a Polygon. return new Polygon(Point.getConvexHull(getPoints())); } @Override public void onDraw(Canvas canvas) { Context2d context = canvas.getContext2d(); // This is the code to display the computed convex hull. // Compute the convex hull. Polygon convexHull = getShape(); for (Line s : convexHull.getOutlineSegments()) { Point p1 = s.getP1(); Point p2 = s.getP2(); context.moveTo(p1.x, p1.y); context.lineTo(p2.x, p2.y); context.stroke(); } // Display the convex hull as an SWT Path. // gc.drawPath(new org.eclipse.swt.graphics.Path(Display // .getCurrent(), Geometry2SWT.toSWTPathData(convexHull // .toPath()))); } }; return new ControllableShape[] { shape }; } }