JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / java / awt / Polygon.js
1 Clazz.declarePackage ("java.awt");\r
2 Clazz.load (["java.awt.Shape", "java.awt.geom.PathIterator"], "java.awt.Polygon", ["java.lang.IndexOutOfBoundsException", "$.NegativeArraySizeException", "java.util.Arrays", "java.awt.Rectangle", "jssun.awt.geom.Crossings"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.npoints = 0;\r
5 this.xpoints = null;\r
6 this.ypoints = null;\r
7 this.bounds = null;\r
8 if (!Clazz.isClassDefined ("java.awt.Polygon.PolygonPathIterator")) {\r
9 java.awt.Polygon.$Polygon$PolygonPathIterator$ ();\r
10 }\r
11 Clazz.instantialize (this, arguments);\r
12 }, java.awt, "Polygon", null, java.awt.Shape);\r
13 Clazz.makeConstructor (c$, \r
14 function () {\r
15 this.xpoints =  Clazz.newIntArray (4, 0);\r
16 this.ypoints =  Clazz.newIntArray (4, 0);\r
17 });\r
18 Clazz.makeConstructor (c$, \r
19 function (xpoints, ypoints, npoints) {\r
20 if (npoints > xpoints.length || npoints > ypoints.length) {\r
21 throw  new IndexOutOfBoundsException ("npoints > xpoints.length || npoints > ypoints.length");\r
22 }if (npoints < 0) {\r
23 throw  new NegativeArraySizeException ("npoints < 0");\r
24 }this.npoints = npoints;\r
25 this.xpoints = java.util.Arrays.copyOf (xpoints, npoints);\r
26 this.ypoints = java.util.Arrays.copyOf (ypoints, npoints);\r
27 }, "~A,~A,~N");\r
28 Clazz.defineMethod (c$, "reset", \r
29 function () {\r
30 this.npoints = 0;\r
31 this.bounds = null;\r
32 });\r
33 Clazz.defineMethod (c$, "invalidate", \r
34 function () {\r
35 this.bounds = null;\r
36 });\r
37 Clazz.defineMethod (c$, "translate", \r
38 function (deltaX, deltaY) {\r
39 for (var i = 0; i < this.npoints; i++) {\r
40 this.xpoints[i] += deltaX;\r
41 this.ypoints[i] += deltaY;\r
42 }\r
43 if (this.bounds != null) {\r
44 this.bounds.translate (deltaX, deltaY);\r
45 }}, "~N,~N");\r
46 Clazz.defineMethod (c$, "calculateBounds", \r
47 function (xpoints, ypoints, npoints) {\r
48 var boundsMinX = 2147483647;\r
49 var boundsMinY = 2147483647;\r
50 var boundsMaxX = -2147483648;\r
51 var boundsMaxY = -2147483648;\r
52 for (var i = 0; i < npoints; i++) {\r
53 var x = xpoints[i];\r
54 boundsMinX = Math.min (boundsMinX, x);\r
55 boundsMaxX = Math.max (boundsMaxX, x);\r
56 var y = ypoints[i];\r
57 boundsMinY = Math.min (boundsMinY, y);\r
58 boundsMaxY = Math.max (boundsMaxY, y);\r
59 }\r
60 this.bounds =  new java.awt.Rectangle (boundsMinX, boundsMinY, boundsMaxX - boundsMinX, boundsMaxY - boundsMinY);\r
61 }, "~A,~A,~N");\r
62 Clazz.defineMethod (c$, "updateBounds", \r
63 function (x, y) {\r
64 if (x < this.bounds.x) {\r
65 this.bounds.width = this.bounds.width + (this.bounds.x - x);\r
66 this.bounds.x = x;\r
67 } else {\r
68 this.bounds.width = Math.max (this.bounds.width, x - this.bounds.x);\r
69 }if (y < this.bounds.y) {\r
70 this.bounds.height = this.bounds.height + (this.bounds.y - y);\r
71 this.bounds.y = y;\r
72 } else {\r
73 this.bounds.height = Math.max (this.bounds.height, y - this.bounds.y);\r
74 }}, "~N,~N");\r
75 Clazz.defineMethod (c$, "addPoint", \r
76 function (x, y) {\r
77 if (this.npoints >= this.xpoints.length || this.npoints >= this.ypoints.length) {\r
78 var newLength = this.npoints * 2;\r
79 if (newLength < 4) {\r
80 newLength = 4;\r
81 } else if ((newLength & (newLength - 1)) != 0) {\r
82 newLength = Integer.highestOneBit (newLength);\r
83 }this.xpoints = java.util.Arrays.copyOf (this.xpoints, newLength);\r
84 this.ypoints = java.util.Arrays.copyOf (this.ypoints, newLength);\r
85 }this.xpoints[this.npoints] = x;\r
86 this.ypoints[this.npoints] = y;\r
87 this.npoints++;\r
88 if (this.bounds != null) {\r
89 this.updateBounds (x, y);\r
90 }}, "~N,~N");\r
91 Clazz.overrideMethod (c$, "getBounds", \r
92 function () {\r
93 return this.getBoundingBox ();\r
94 });\r
95 Clazz.defineMethod (c$, "getBoundingBox", \r
96 function () {\r
97 if (this.npoints == 0) {\r
98 return  new java.awt.Rectangle ();\r
99 }if (this.bounds == null) {\r
100 this.calculateBounds (this.xpoints, this.ypoints, this.npoints);\r
101 }return this.bounds.getBounds ();\r
102 });\r
103 Clazz.defineMethod (c$, "contains", \r
104 function (p) {\r
105 return this.contains (p.x, p.y);\r
106 }, "java.awt.Point");\r
107 Clazz.defineMethod (c$, "contains", \r
108 function (x, y) {\r
109 return this.contains (x, y);\r
110 }, "~N,~N");\r
111 Clazz.defineMethod (c$, "inside", \r
112 function (x, y) {\r
113 return this.contains (x, y);\r
114 }, "~N,~N");\r
115 Clazz.overrideMethod (c$, "getBounds2D", \r
116 function () {\r
117 return this.getBounds ();\r
118 });\r
119 Clazz.defineMethod (c$, "contains", \r
120 function (x, y) {\r
121 if (this.npoints <= 2 || !this.getBoundingBox ().contains (x, y)) {\r
122 return false;\r
123 }var hits = 0;\r
124 var lastx = this.xpoints[this.npoints - 1];\r
125 var lasty = this.ypoints[this.npoints - 1];\r
126 var curx;\r
127 var cury;\r
128 for (var i = 0; i < this.npoints; lastx = curx, lasty = cury, i++) {\r
129 curx = this.xpoints[i];\r
130 cury = this.ypoints[i];\r
131 if (cury == lasty) {\r
132 continue;\r
133 }var leftx;\r
134 if (curx < lastx) {\r
135 if (x >= lastx) {\r
136 continue;\r
137 }leftx = curx;\r
138 } else {\r
139 if (x >= curx) {\r
140 continue;\r
141 }leftx = lastx;\r
142 }var test1;\r
143 var test2;\r
144 if (cury < lasty) {\r
145 if (y < cury || y >= lasty) {\r
146 continue;\r
147 }if (x < leftx) {\r
148 hits++;\r
149 continue;\r
150 }test1 = x - curx;\r
151 test2 = y - cury;\r
152 } else {\r
153 if (y < lasty || y >= cury) {\r
154 continue;\r
155 }if (x < leftx) {\r
156 hits++;\r
157 continue;\r
158 }test1 = x - lastx;\r
159 test2 = y - lasty;\r
160 }if (test1 < (test2 / (lasty - cury) * (lastx - curx))) {\r
161 hits++;\r
162 }}\r
163 return ((hits & 1) != 0);\r
164 }, "~N,~N");\r
165 Clazz.defineMethod (c$, "getCrossings", \r
166  function (xlo, ylo, xhi, yhi) {\r
167 var cross =  new jssun.awt.geom.Crossings.EvenOdd (xlo, ylo, xhi, yhi);\r
168 var lastx = this.xpoints[this.npoints - 1];\r
169 var lasty = this.ypoints[this.npoints - 1];\r
170 var curx;\r
171 var cury;\r
172 for (var i = 0; i < this.npoints; i++) {\r
173 curx = this.xpoints[i];\r
174 cury = this.ypoints[i];\r
175 if (cross.accumulateLine (lastx, lasty, curx, cury)) {\r
176 return null;\r
177 }lastx = curx;\r
178 lasty = cury;\r
179 }\r
180 return cross;\r
181 }, "~N,~N,~N,~N");\r
182 Clazz.defineMethod (c$, "contains", \r
183 function (p) {\r
184 return this.contains (p.getX (), p.getY ());\r
185 }, "java.awt.geom.Point2D");\r
186 Clazz.defineMethod (c$, "intersects", \r
187 function (x, y, w, h) {\r
188 if (this.npoints <= 0 || !this.getBoundingBox ().intersects (x, y, w, h)) {\r
189 return false;\r
190 }var cross = this.getCrossings (x, y, x + w, y + h);\r
191 return (cross == null || !cross.isEmpty ());\r
192 }, "~N,~N,~N,~N");\r
193 Clazz.defineMethod (c$, "intersects", \r
194 function (r) {\r
195 return this.intersects (r.getX (), r.getY (), r.getWidth (), r.getHeight ());\r
196 }, "java.awt.geom.Rectangle2D");\r
197 Clazz.defineMethod (c$, "contains", \r
198 function (x, y, w, h) {\r
199 if (this.npoints <= 0 || !this.getBoundingBox ().intersects (x, y, w, h)) {\r
200 return false;\r
201 }var cross = this.getCrossings (x, y, x + w, y + h);\r
202 return (cross != null && cross.covers (y, y + h));\r
203 }, "~N,~N,~N,~N");\r
204 Clazz.defineMethod (c$, "contains", \r
205 function (r) {\r
206 return this.contains (r.getX (), r.getY (), r.getWidth (), r.getHeight ());\r
207 }, "java.awt.geom.Rectangle2D");\r
208 Clazz.defineMethod (c$, "getPathIterator", \r
209 function (at) {\r
210 return Clazz.innerTypeInstance (java.awt.Polygon.PolygonPathIterator, this, null, this, at);\r
211 }, "java.awt.geom.AffineTransform");\r
212 Clazz.defineMethod (c$, "getPathIterator", \r
213 function (at, flatness) {\r
214 return this.getPathIterator (at);\r
215 }, "java.awt.geom.AffineTransform,~N");\r
216 c$.$Polygon$PolygonPathIterator$ = function () {\r
217 Clazz.pu$h(self.c$);\r
218 c$ = Clazz.decorateAsClass (function () {\r
219 Clazz.prepareCallback (this, arguments);\r
220 this.poly = null;\r
221 this.transform = null;\r
222 this.index = 0;\r
223 Clazz.instantialize (this, arguments);\r
224 }, java.awt.Polygon, "PolygonPathIterator", null, java.awt.geom.PathIterator);\r
225 Clazz.makeConstructor (c$, \r
226 function (a, b) {\r
227 this.poly = a;\r
228 this.transform = b;\r
229 if (a.npoints == 0) {\r
230 this.index = 1;\r
231 }}, "java.awt.Polygon,java.awt.geom.AffineTransform");\r
232 Clazz.overrideMethod (c$, "getWindingRule", \r
233 function () {\r
234 return 0;\r
235 });\r
236 Clazz.overrideMethod (c$, "isDone", \r
237 function () {\r
238 return this.index > this.poly.npoints;\r
239 });\r
240 Clazz.overrideMethod (c$, "next", \r
241 function () {\r
242 this.index++;\r
243 });\r
244 Clazz.defineMethod (c$, "currentSegment", \r
245 function (a) {\r
246 if (this.index >= this.poly.npoints) {\r
247 return 4;\r
248 }a[0] = this.poly.xpoints[this.index];\r
249 a[1] = this.poly.ypoints[this.index];\r
250 if (this.transform != null) {\r
251 this.transform.transform (a, 0, a, 0, 1);\r
252 }return (this.index == 0 ? 0 : 1);\r
253 }, "~A");\r
254 c$ = Clazz.p0p ();\r
255 };\r
256 Clazz.defineStatics (c$,\r
257 "MIN_LENGTH", 4);\r
258 });\r