Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: be576ab3d6598dedf21c77956891631dd3277a31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*****************************************************************************
 * 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:
 *  Emilien Perico (Atos Origin) emilien.perico@atosorigin.com - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.diagram.usecase.draw2d;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;

/**
 * The Class StickMan. This class comes from org.eclipse.uml2.diagram.usecase.draw2d provided by
 * Eclipse.org
 */
public class StickMan extends ShadowShape {

	private static final float BASE_W = 31 - 1;

	private static final float BASE_H = 50 - 1;

	private static final float FACTOR1 = 16f / BASE_H;

	private static final float FACTOR2 = 22f / BASE_H;

	private static final int P_NUM = 20;

	private int ovalX;

	private int ovalY;

	private int ovalD;

	public StickMan() {
		this(false, ColorConstants.white, ColorConstants.black);
	}

	public StickMan(boolean is3D, Color backgroundColor, Color foregroundColor) {
		super(is3D, backgroundColor, foregroundColor);
		setKeepingProportions(true);
		setW2HRatio(BASE_W / BASE_H);
	}

	/**
	 * Outlines the ellipse.
	 */
	protected void outlineShape(Graphics graphics, Rectangle bounds) {
		PointList pl = setupPoints(bounds);
		graphics.drawPolygon(pl);
		int add = graphics.getLineWidth() / 2;
		graphics.drawOval(new Rectangle(ovalX, ovalY, ovalD + add, ovalD + add));
	}

	/**
	 * Fills the ellipse.
	 */
	protected void fillShape(Graphics graphics, Rectangle bounds) {
		PointList pl = setupPoints(bounds);
		graphics.fillPolygon(pl);
		int add = graphics.getLineWidth() / 2;
		graphics.fillOval(new Rectangle(ovalX, ovalY, ovalD + add, ovalD + add));
	}

	/**
	 * Setup the points to draw the stickMan figure.
	 * 
	 * @param rectangle
	 *        the specified rectangle
	 * 
	 * @return the point list
	 */
	protected PointList setupPoints(Rectangle rectangle) {
		int[] xPoints = new int[P_NUM];
		int[] yPoints = new int[P_NUM];

		PointList pl = new PointList(10);
		int W = (rectangle.width / 2) * 2;
		int H = rectangle.height;
		int X1 = W / 2;
		int Y1 = (Math.round(H * FACTOR1) / 2) * 2;
		int Y2 = Math.round(H * FACTOR2);
		int Y3 = H - (X1 - 1);
		int STEP = Math.round(W / BASE_W);
		if(STEP < 1) {
			STEP = 1;
		}

		// set positive points. (0...9)
		xPoints[0] = STEP;
		yPoints[0] = Y1;
		xPoints[1] = STEP;
		yPoints[1] = Y2 - STEP;
		xPoints[2] = X1;
		yPoints[2] = Y2 - STEP;
		xPoints[3] = X1;
		yPoints[3] = Y2 + STEP;
		xPoints[4] = STEP;
		yPoints[4] = Y2 + STEP;
		xPoints[5] = STEP;
		yPoints[5] = Y3 - STEP;
		xPoints[6] = X1;
		yPoints[6] = H - STEP;
		xPoints[7] = X1;
		yPoints[7] = H;
		xPoints[8] = X1 - 2 * STEP;
		yPoints[8] = H;
		xPoints[9] = 0;
		yPoints[9] = Y3 + STEP;

		// reflect points 0..8
		for(int i = 0; i <= 8; i++) {
			xPoints[18 - i] = -xPoints[i];
			yPoints[18 - i] = yPoints[i];
		}

		// close polyline.
		xPoints[19] = xPoints[0];
		yPoints[19] = yPoints[0];

		// shift all points and copy to integer.
		for(int i = 0; i < P_NUM; i++) {
			xPoints[i] += X1;

			xPoints[i] += rectangle.x;
			yPoints[i] += rectangle.y;
		}

		for(int i = 0; i < xPoints.length; i++) {
			pl.addPoint(xPoints[i], yPoints[i]);
		}

		// head-oval
		ovalD = Y1;
		ovalX = X1 - ovalD / 2 + rectangle.x;
		ovalY = rectangle.y;

		return pl;
	}

}

Back to the top