2
2
3
3
import java .util .Collection ;
4
4
import java .util .ArrayList ;
5
+
5
6
import org .apache .commons .lang .StringUtils ;
7
+
6
8
import java .awt .Rectangle ;
7
9
public class Coordinates {
8
10
@@ -14,6 +16,51 @@ public Coordinates() {
14
16
public Coordinates (Collection <Rectangle > coordinates ) {
15
17
this .coordinates = coordinates ;
16
18
}
19
+
20
+ public Coordinates (int [] rect ) {
21
+ Collection <Rectangle > coordinates = new ArrayList <Rectangle >();
22
+ if (rect .length != 4 ) {
23
+ throw new IllegalArgumentException ("Must supply exactly 4 values for coordinates (x,y,width,height)" );
24
+ }
25
+ coordinates .add (new Rectangle (rect [0 ], rect [1 ], rect [2 ], rect [3 ]));
26
+ this .coordinates = coordinates ;
27
+ }
28
+
29
+ public Coordinates (Rectangle rect ) {
30
+ Collection <Rectangle > coordinates = new ArrayList <Rectangle >();
31
+ coordinates .add (rect );
32
+ this .coordinates = coordinates ;
33
+ }
34
+
35
+ public Coordinates (String stringCoords ) throws IllegalArgumentException {
36
+ Collection <Rectangle > coordinates = new ArrayList <Rectangle >();
37
+ for (String stringRect : stringCoords .split ("\\ |" )) {
38
+ if (stringRect .isEmpty ()) continue ;
39
+ String [] elements = stringRect .split ("," );
40
+ if (elements .length != 4 ) {
41
+ throw new IllegalArgumentException (String .format ("Must supply exactly 4 values for coordinates (x,y,width,height) %d supplied: %s" , elements .length , stringRect ));
42
+ }
43
+ coordinates .add (new Rectangle (
44
+ Integer .parseInt (elements [0 ]),
45
+ Integer .parseInt (elements [1 ]),
46
+ Integer .parseInt (elements [2 ]),
47
+ Integer .parseInt (elements [3 ])));
48
+ }
49
+ this .coordinates = coordinates ;
50
+ }
51
+
52
+
53
+ public static Coordinates parseCoordinates (Object coordinates ) throws IllegalArgumentException {
54
+ if (coordinates instanceof Coordinates ) {
55
+ return (Coordinates )coordinates ;
56
+ } else if (coordinates instanceof int []) {
57
+ return new Coordinates ((int []) coordinates );
58
+ } else if (coordinates instanceof Rectangle ) {
59
+ return new Coordinates ((Rectangle ) coordinates );
60
+ } else {
61
+ return new Coordinates (coordinates .toString ());
62
+ }
63
+ }
17
64
18
65
public void addRect (Rectangle rect ) {
19
66
this .coordinates .add (rect );
0 commit comments