|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| package org.ericmignot.modeler.util.graphic; |
|
20 |
| |
|
21 |
| import java.awt.*; |
|
22 |
| import java.awt.geom.*; |
|
23 |
| |
|
24 |
| import javax.swing.*; |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| public class GraphicHelper |
|
30 |
| { |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
1499
| public static Point getPanelCenter(JPanel panel)
|
|
38 |
| { |
|
39 |
1499
| Rectangle bounds = panel.getBounds();
|
|
40 |
1499
| Point toReturn = new Point(
|
|
41 |
| bounds.x + bounds.width/2, |
|
42 |
| bounds.y + bounds.height/2); |
|
43 |
1499
| return toReturn;
|
|
44 |
| } |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
1
| public static Rectangle getMinPanelFromPoints(Point[] points)
|
|
57 |
| { |
|
58 |
1
| if (points.length == 0)
|
|
59 |
| { |
|
60 |
1
| return new Rectangle(0, 0, 0, 0);
|
|
61 |
| } |
|
62 |
0
| int minX = 60000;
|
|
63 |
0
| int maxX = 0;
|
|
64 |
0
| int minY = 60000;
|
|
65 |
0
| int maxY = 0;
|
|
66 |
| |
|
67 |
0
| for (int i = 0; i < points.length; i ++)
|
|
68 |
| { |
|
69 |
0
| minX = (int) Math.min(minX, points[i].getX());
|
|
70 |
0
| minY = (int) Math.min(minY, points[i].getY());
|
|
71 |
0
| maxX = (int) Math.max(maxX, points[i].getX());
|
|
72 |
0
| maxY = (int) Math.max(maxY, points[i].getY());
|
|
73 |
| } |
|
74 |
0
| Rectangle toReturn = new Rectangle(minX, minY, (maxX-minX+1), (maxY-minY+1));
|
|
75 |
0
| return toReturn;
|
|
76 |
| } |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
625
| public static Point getLineRectangleIntersection(Point point1, Point point2, Rectangle rectangle)
|
|
86 |
| { |
|
87 |
625
| int outCode = rectangle.outcode(point1.getX(), point1.getY());
|
|
88 |
| |
|
89 |
625
| Point arrowPoint = null;
|
|
90 |
625
| if (outCode == Rectangle2D.OUT_LEFT)
|
|
91 |
| { |
|
92 |
184
| int x = (int) rectangle.getX();
|
|
93 |
| |
|
94 |
184
| int y = (int) (point1.getY()*(point2.getX()-x)/(point2.getX()-point1.getX()) + point2.getY()*(point1.getX()-x)/(point1.getX()-point2.getX()));
|
|
95 |
184
| arrowPoint = new Point(x, y);
|
|
96 |
| } |
|
97 |
441
| else if (outCode == Rectangle2D.OUT_RIGHT)
|
|
98 |
| { |
|
99 |
178
| int x = (int) (rectangle.getX() + rectangle.getWidth() - 1);
|
|
100 |
| |
|
101 |
178
| int y = (int) (point1.getY()*(point2.getX()-x)/(point2.getX()-point1.getX()) + point2.getY()*(point1.getX()-x)/(point1.getX()-point2.getX()));
|
|
102 |
178
| arrowPoint = new Point(x, y);
|
|
103 |
| } |
|
104 |
263
| else if (outCode == Rectangle2D.OUT_TOP)
|
|
105 |
| { |
|
106 |
4
| int y = (int) (rectangle.getY());
|
|
107 |
| |
|
108 |
4
| int x = (int) (point1.getX()*(point2.getY()-y)/(point2.getY()-point1.getY()) + point2.getX()*(point1.getY()-y)/(point1.getY()-point2.getY()));
|
|
109 |
4
| arrowPoint = new Point(x, y);
|
|
110 |
| } |
|
111 |
259
| else if (outCode == Rectangle2D.OUT_BOTTOM)
|
|
112 |
| { |
|
113 |
4
| int y = (int) (rectangle.getY() + rectangle.getHeight() - 1);
|
|
114 |
| |
|
115 |
4
| int x = (int) (point1.getX()*(point2.getY()-y)/(point2.getY()-point1.getY()) + point2.getX()*(point1.getY()-y)/(point1.getY()-point2.getY()));
|
|
116 |
4
| arrowPoint = new Point(x, y);
|
|
117 |
| } |
|
118 |
255
| else if (outCode == Rectangle2D.OUT_LEFT + Rectangle.OUT_TOP)
|
|
119 |
| { |
|
120 |
24
| int ccw = Line2D.relativeCCW(point1.getX(), point1.getY(), point2.getX(), point2.getY(),
|
|
121 |
| rectangle.getX(), rectangle.getY()); |
|
122 |
24
| if (ccw == -1)
|
|
123 |
| { |
|
124 |
0
| int y = (int) (rectangle.getY());
|
|
125 |
| |
|
126 |
0
| int x = (int) (point1.getX()*(point2.getY()-y)/(point2.getY()-point1.getY()) + point2.getX()*(point1.getY()-y)/(point1.getY()-point2.getY()));
|
|
127 |
0
| arrowPoint = new Point(x, y);
|
|
128 |
| } |
|
129 |
| else |
|
130 |
| { |
|
131 |
24
| int x = (int) rectangle.getX();
|
|
132 |
| |
|
133 |
24
| int y = (int) (point1.getY()*(point2.getX()-x)/(point2.getX()-point1.getX()) + point2.getY()*(point1.getX()-x)/(point1.getX()-point2.getX()));
|
|
134 |
24
| arrowPoint = new Point(x, y);
|
|
135 |
| } |
|
136 |
| } |
|
137 |
231
| else if (outCode == Rectangle2D.OUT_LEFT + Rectangle.OUT_BOTTOM)
|
|
138 |
| { |
|
139 |
4
| int ccw = Line2D.relativeCCW(point1.getX(), point1.getY(), point2.getX(), point2.getY(),
|
|
140 |
| rectangle.getX(), rectangle.getY() + rectangle.getHeight() - 1); |
|
141 |
4
| if (ccw == -1)
|
|
142 |
| { |
|
143 |
2
| int x = (int) rectangle.getX();
|
|
144 |
| |
|
145 |
2
| int y = (int) (point1.getY()*(point2.getX()-x)/(point2.getX()-point1.getX()) + point2.getY()*(point1.getX()-x)/(point1.getX()-point2.getX()));
|
|
146 |
2
| arrowPoint = new Point(x, y);
|
|
147 |
| } |
|
148 |
| else |
|
149 |
| { |
|
150 |
2
| int y = (int) (rectangle.getY() + rectangle.getHeight() - 1);
|
|
151 |
| |
|
152 |
2
| int x = (int) (point1.getX()*(point2.getY()-y)/(point2.getY()-point1.getY()) + point2.getX()*(point1.getY()-y)/(point1.getY()-point2.getY()));
|
|
153 |
2
| arrowPoint = new Point(x, y);
|
|
154 |
| } |
|
155 |
| } |
|
156 |
227
| else if (outCode == Rectangle2D.OUT_RIGHT + Rectangle.OUT_TOP)
|
|
157 |
| { |
|
158 |
4
| int ccw = Line2D.relativeCCW(point1.getX(), point1.getY(), point2.getX(), point2.getY(),
|
|
159 |
| rectangle.getX() + rectangle.getWidth() - 1, rectangle.getY()); |
|
160 |
4
| if (ccw == -1)
|
|
161 |
| { |
|
162 |
1
| int x = (int) (rectangle.getX() + rectangle.getWidth() - 1);
|
|
163 |
| |
|
164 |
1
| int y = (int) (point1.getY()*(point2.getX()-x)/(point2.getX()-point1.getX()) + point2.getY()*(point1.getX()-x)/(point1.getX()-point2.getX()));
|
|
165 |
1
| arrowPoint = new Point(x, y);
|
|
166 |
| } |
|
167 |
| else |
|
168 |
| { |
|
169 |
3
| int y = (int) (rectangle.getY());
|
|
170 |
| |
|
171 |
3
| int x = (int) (point1.getX()*(point2.getY()-y)/(point2.getY()-point1.getY()) + point2.getX()*(point1.getY()-y)/(point1.getY()-point2.getY()));
|
|
172 |
3
| arrowPoint = new Point(x, y);
|
|
173 |
| } |
|
174 |
| } |
|
175 |
223
| else if (outCode == Rectangle2D.OUT_RIGHT + Rectangle.OUT_BOTTOM)
|
|
176 |
| { |
|
177 |
72
| int ccw = Line2D.relativeCCW(point1.getX(), point1.getY(), point2.getX(), point2.getY(),
|
|
178 |
| rectangle.getX() + rectangle.getWidth() - 1, rectangle.getY() + rectangle.getHeight() - 1); |
|
179 |
72
| if (ccw == -1)
|
|
180 |
| { |
|
181 |
0
| int y = (int) (rectangle.getY() + rectangle.getHeight() - 1);
|
|
182 |
| |
|
183 |
0
| int x = (int) (point1.getX()*(point2.getY()-y)/(point2.getY()-point1.getY()) + point2.getX()*(point1.getY()-y)/(point1.getY()-point2.getY()));
|
|
184 |
0
| arrowPoint = new Point(x, y);
|
|
185 |
| } |
|
186 |
| else |
|
187 |
| { |
|
188 |
72
| int x = (int) (rectangle.getX() + rectangle.getWidth() - 1);
|
|
189 |
| |
|
190 |
72
| int y = (int) (point1.getY()*(point2.getX()-x)/(point2.getX()-point1.getX()) + point2.getY()*(point1.getX()-x)/(point1.getX()-point2.getX()));
|
|
191 |
72
| arrowPoint = new Point(x, y);
|
|
192 |
| } |
|
193 |
| } |
|
194 |
625
| return arrowPoint;
|
|
195 |
| } |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
0
| public static void drawArrow(Graphics g, Point point1, Point point2, double zoomFactor)
|
|
206 |
| { |
|
207 |
0
| double angle = 30 * Math.PI / 180;
|
|
208 |
0
| double length = 10 * zoomFactor;
|
|
209 |
| |
|
210 |
0
| double norme = Math.sqrt((point2.getY() - point1.getY())*(point2.getY() - point1.getY()) + (point2.getX() - point1.getX())*(point2.getX() - point1.getX()));
|
|
211 |
0
| Point2D unitVector = new Point2D.Double(
|
|
212 |
| (point1.getX()-point2.getX())/norme, |
|
213 |
| (point1.getY()-point2.getY())/norme |
|
214 |
| ); |
|
215 |
0
| double cos = Math.cos(angle);
|
|
216 |
0
| double sin = Math.sin(angle);
|
|
217 |
| |
|
218 |
0
| double x;
|
|
219 |
0
| double y;
|
|
220 |
| |
|
221 |
0
| x = cos * unitVector.getX() - sin * unitVector.getY();
|
|
222 |
0
| y = sin * unitVector.getX() + cos * unitVector.getY();
|
|
223 |
0
| x *= length;
|
|
224 |
0
| y *= length;
|
|
225 |
0
| x += point2.getX();
|
|
226 |
0
| y += point2.getY();
|
|
227 |
0
| Point base1 = new Point((int) x, (int) y);
|
|
228 |
| |
|
229 |
| |
|
230 |
0
| x = cos * unitVector.getX() + sin * unitVector.getY();
|
|
231 |
0
| y = -sin * unitVector.getX() + cos * unitVector.getY();
|
|
232 |
0
| x *= length;
|
|
233 |
0
| y *= length;
|
|
234 |
0
| x += point2.getX();
|
|
235 |
0
| y += point2.getY();
|
|
236 |
0
| Point base2 = new Point((int) x, (int) y);
|
|
237 |
| |
|
238 |
| |
|
239 |
0
| Polygon poly = new Polygon(new int[] {base1.x, point2.x, base2.x},
|
|
240 |
| new int[] {base1.y, point2.y, base2.y}, 3); |
|
241 |
0
| g.fillPolygon(poly);
|
|
242 |
| |
|
243 |
| } |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
0
| public static Rectangle enlargeRectangle(Rectangle rectangle, int semiWidth, int semiHeight)
|
|
253 |
| { |
|
254 |
0
| rectangle.setBounds(
|
|
255 |
| (int)(rectangle.getX() - semiWidth), |
|
256 |
| (int)(rectangle.getY() - semiHeight), |
|
257 |
| (int)(rectangle.getWidth() + 2*semiWidth), |
|
258 |
| (int)(rectangle.getHeight() + 2*semiHeight) |
|
259 |
| ); |
|
260 |
0
| return rectangle;
|
|
261 |
| } |
|
262 |
| |
|
263 |
3
| public static Rectangle buildContainerRectangle(Rectangle r1, Rectangle r2)
|
|
264 |
| { |
|
265 |
3
| int minx = Math.min(r1.x, r2.x);
|
|
266 |
3
| int miny = Math.min(r1.y, r2.y);
|
|
267 |
3
| int maxx = Math.max(r1.x+r1.width, r2.x+r2.width);
|
|
268 |
3
| int maxy = Math.max(r1.y+r1.height, r2.y+r2.height);
|
|
269 |
| |
|
270 |
3
| return new Rectangle(minx, miny, (maxx-minx), (maxy-miny));
|
|
271 |
| } |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
491
| public static Color buildColor(String rvb)
|
|
279 |
| { |
|
280 |
491
| String[] values = rvb.split(",");
|
|
281 |
| |
|
282 |
491
| int red = Integer.parseInt(values[0].trim());
|
|
283 |
491
| int green = Integer.parseInt(values[1].trim());
|
|
284 |
491
| int blue = Integer.parseInt(values[2].trim());
|
|
285 |
| |
|
286 |
491
| return new Color(red, green, blue);
|
|
287 |
| } |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
0
| public static double getSegmentDistance(Point s1, Point s2, Point p)
|
|
297 |
| { |
|
298 |
0
| double toReturn = -1;
|
|
299 |
| |
|
300 |
0
| Rectangle r = new Rectangle(Math.min(s1.x, s2.x), Math.min(s1.y, s2.y),
|
|
301 |
| Math.abs(s2.x-s1.x +1), Math.abs(s2.y-s1.y +1)); |
|
302 |
0
| if (r.contains(p))
|
|
303 |
| { |
|
304 |
0
| toReturn = Line2D.ptLineDist(s1.x, s1.y, s2.x, s2.y, p.x, p.y);
|
|
305 |
| } |
|
306 |
| |
|
307 |
0
| return toReturn;
|
|
308 |
| } |
|
309 |
77
| public static double getLineDistance(Point s1, Point s2, Point p)
|
|
310 |
| { |
|
311 |
77
| return Line2D.ptLineDist(s1.x, s1.y, s2.x, s2.y, p.x, p.y);
|
|
312 |
| } |
|
313 |
| } |