Java tutorial
/******************************************************************************* * 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.Line; import org.eclipse.gef4.geometry.planar.Point; import org.eclipse.gef4.geometry.planar.Polygon; import org.eclipse.gef4.geometry.planar.Polygon.NonSimplePolygonException; 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 TriangulationExample extends AbstractExample { public TriangulationExample(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(300 / 2, 100 / 2), new Point(100 / 2, 200 / 2), new Point(200 / 2, 300 / 2), new Point(100 / 2, 500 / 2), new Point(300 / 2, 400 / 2), new Point(500 / 2, 600 / 2), new Point(600 / 2, 300 / 2), new Point(500 / 2, 400 / 2), new Point(500 / 2, 200 / 2), new Point(300 / 2, 200 / 2)); } @Override public Polygon getShape() { Polygon p = new Polygon(getPoints()); return p; } @Override public void onDraw(Canvas canvas) { Context2d context = canvas.getContext2d(); Polygon p = getShape(); Polygon[] triangulation; try { triangulation = p.getTriangulation(); } catch (NonSimplePolygonException x) { triangulation = new Polygon[] { p }; } for (Polygon triangle : triangulation) { context.beginPath(); context.setStrokeStyle("red"); for (Line s : triangle.getOutlineSegments()) { Point p1 = s.getP1(); Point p2 = s.getP2(); context.moveTo(p1.x, p1.y); context.lineTo(p2.x, p2.y); context.stroke(); } context.closePath(); } double lineWidth = context.getLineWidth(); context.setLineWidth(lineWidth + 2); context.setStrokeStyle("black"); context.beginPath(); for (Line s : p.getOutlineSegments()) { Point p1 = s.getP1(); Point p2 = s.getP2(); context.moveTo(p1.x, p1.y); context.lineTo(p2.x, p2.y); context.stroke(); } context.closePath(); context.setLineWidth(lineWidth); } } }; } }