Project

General

Profile

Download (1.99 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.gef.editpolicies;
12

    
13
import java.awt.BasicStroke;
14
import java.awt.Shape;
15
import java.awt.geom.Area;
16
import java.awt.geom.GeneralPath;
17
import java.awt.geom.PathIterator;
18

    
19
import org.eclipse.draw2d.geometry.Point;
20
import org.eclipse.draw2d.geometry.PointList;
21

    
22
/**
23
 * @author hudsonr
24
 * @since 2.0
25
 */
26
public class StrokePointList {
27

    
28
	static float segment[] = new float[6];
29

    
30
	static PointList strokeList(PointList list, int offset) {
31
		GeneralPath path = new GeneralPath(GeneralPath.WIND_NON_ZERO);
32

    
33
		Point p = list.getPoint(0);
34
		path.moveTo(p.x, p.y);
35
		for (int i = 1; i < list.size(); i++)
36
			path.lineTo((p = list.getPoint(i)).x, p.y);
37
		BasicStroke stroke = new BasicStroke(offset * 2,
38
				BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f);
39
		Shape stroked = stroke.createStrokedShape(path);
40
		Area area = new Area(stroked);
41
		PathIterator iter = area.getPathIterator(null, 10.0f);
42

    
43
		PointList currentSegment = null;
44
		PointList result = null;
45
		int largestSegmentSize = 0;
46

    
47
		while (!iter.isDone()) {
48
			if (currentSegment == null)
49
				currentSegment = new PointList(list.size() * 2);
50
			int type = iter.currentSegment(segment);
51
			currentSegment.addPoint(Math.round(segment[0]),
52
					Math.round(segment[1]));
53
			iter.next();
54
			if (type == PathIterator.SEG_CLOSE) {
55
				if (currentSegment.size() > largestSegmentSize) {
56
					result = currentSegment;
57
					largestSegmentSize = currentSegment.size();
58
					currentSegment = null;
59
				}
60
			}
61
		}
62

    
63
		return result;
64
	}
65

    
66
}
(23-23/26)