1 Clazz.declarePackage ("swingjs");
2 Clazz.load (["jssun.java2d.SunGraphics2D"], "swingjs.JSGraphics2D", ["java.util.HashMap", "java.awt.BasicStroke", "$.Rectangle", "$.RenderingHints", "$.Toolkit", "java.awt.geom.AffineTransform", "swingjs.JSToolkit", "swingjs.api.HTML5CanvasContext2D"], function () {
3 c$ = Clazz.decorateAsClass (function () {
12 this.compositeState = -2147483648;
14 this.$transformState = 0;
16 this.isShifted = false;
19 this.currentClip = null;
20 Clazz.instantialize (this, arguments);
21 }, swingjs, "JSGraphics2D", jssun.java2d.SunGraphics2D, Cloneable);
22 Clazz.makeConstructor (c$,
24 Clazz.superConstructor (this, swingjs.JSGraphics2D, []);
25 this.hints = new java.awt.RenderingHints ( new java.util.HashMap ());
27 this.ctx = this.canvas.getContext ("2d");
28 this.$transform = new java.awt.geom.AffineTransform ();
32 Clazz.overrideMethod (c$, "getDeviceConfiguration",
36 Clazz.overrideMethod (c$, "drawLine",
37 function (x0, y0, x1, y1) {
38 var inPath = this.inPath;
39 if (!inPath) this.ctx.beginPath ();
40 this.ctx.moveTo (x0, y0);
41 this.ctx.lineTo (x1, y1);
42 if (!inPath) this.ctx.stroke ();
44 Clazz.defineMethod (c$, "drawCircle",
45 function (x, y, diameter) {
46 this.drawArc (x, y, diameter, diameter, 0, 360);
48 Clazz.overrideMethod (c$, "fillArc",
49 function (x, y, width, height, startAngle, arcAngle) {
50 this.doArc (x, y, width, height, startAngle, arcAngle, true);
51 }, "~N,~N,~N,~N,~N,~N");
52 Clazz.overrideMethod (c$, "drawArc",
53 function (x, y, width, height, startAngle, arcAngle) {
54 this.doArc (x, y, width, height, startAngle, arcAngle, false);
55 }, "~N,~N,~N,~N,~N,~N");
56 Clazz.defineMethod (c$, "save",
60 Clazz.defineMethod (c$, "restore",
64 Clazz.defineMethod (c$, "doArc",
65 function (x, y, width, height, startAngle, arcAngle, fill) {
66 var isCircle = (arcAngle - startAngle == 360);
68 this.ctx.translate (x, y);
69 this.ctx.scale (Clazz.doubleToInt (width / height), height);
70 this.ctx.beginPath ();
72 }this.ctx.arc (0.5, 0.5, 0.5, this.toRad (startAngle), this.toRad (arcAngle), false);
73 if (isCircle) this.ctx.closePath ();
76 }, "~N,~N,~N,~N,~N,~N,~B");
77 Clazz.defineMethod (c$, "toRad",
79 return a * 3.141592653589793 / 180;
81 Clazz.defineMethod (c$, "drawPolygon",
82 function (ayPoints, axPoints, nPoints) {
83 this.doPoly (ayPoints, axPoints, nPoints, false);
85 Clazz.defineMethod (c$, "doPoly",
86 function (axPoints, ayPoints, nPoints, doFill) {
87 this.ctx.beginPath ();
88 this.ctx.moveTo (axPoints[0], ayPoints[0]);
89 for (var i = 1; i < nPoints; i++) this.ctx.lineTo (axPoints[i], ayPoints[i]);
91 if (doFill) this.ctx.fill ();
92 else this.ctx.stroke ();
94 Clazz.overrideMethod (c$, "drawRect",
95 function (x, y, width, height) {
96 this.ctx.beginPath ();
97 this.ctx.rect (x, y, width, height);
100 Clazz.defineMethod (c$, "background",
102 this.backgroundColor = bgcolor;
103 if (bgcolor == null) {
104 if (!this.isShifted) this.ctx.translate (-0.5, -0.5);
105 this.isShifted = true;
107 }this.ctx.clearRect (0, 0, this.windowWidth, this.windowHeight);
108 this.setGraphicsColor (bgcolor);
109 this.fillRect (0, 0, this.windowWidth, this.windowHeight);
110 }, "java.awt.Color");
111 Clazz.defineMethod (c$, "fillCircle",
112 function (x, y, diameter) {
113 var r = diameter / 2;
114 this.ctx.beginPath ();
115 this.ctx.arc (x + r, y + r, r, 0, 6.283185307179586, false);
118 Clazz.defineMethod (c$, "fillPolygon",
119 function (ayPoints, axPoints, nPoints) {
120 this.doPoly (ayPoints, axPoints, nPoints, true);
122 Clazz.overrideMethod (c$, "fillRect",
123 function (x, y, width, height) {
124 this.ctx.fillRect (x, y, width, height);
126 Clazz.defineMethod (c$, "setGraphicsColor",
128 var s = swingjs.JSToolkit.getCSSColor (c);
130 this.ctx.fillStyle = s; this.ctx.strokeStyle = s;
131 }}, "java.awt.Color");
132 Clazz.overrideMethod (c$, "setFont",
135 if (this.ctx == null) return;
136 var s = swingjs.JSToolkit.getCanvasFont (font);
139 }}, "java.awt.Font");
140 Clazz.defineMethod (c$, "setStrokeBold",
142 this.setLineWidth (tf ? 2. : 1.);
144 Clazz.defineMethod (c$, "setLineWidth",
147 this.ctx.lineWidth = d;
149 Clazz.defineMethod (c$, "setWindowParameters",
150 function (width, height) {
151 this.windowWidth = width;
152 this.windowHeight = height;
154 Clazz.defineMethod (c$, "canDoLineTo",
158 Clazz.defineMethod (c$, "doStroke",
160 this.inPath = isBegin;
162 this.ctx.beginPath ();
166 Clazz.defineMethod (c$, "lineTo",
168 this.ctx.lineTo (x2, y2);
170 Clazz.overrideMethod (c$, "clip",
174 }, "java.awt.Shape");
175 Clazz.overrideMethod (c$, "draw",
179 }, "java.awt.Shape");
180 Clazz.defineMethod (c$, "doShape",
182 this.ctx.beginPath ();
183 var pts = Clazz.newDoubleArray (6, 0);
184 var pi = s.getPathIterator (null);
185 while (!pi.isDone ()) {
186 switch (pi.currentSegment (pts)) {
188 this.ctx.moveTo (pts[0], pts[1]);
191 this.ctx.lineTo (pts[0], pts[1]);
194 this.ctx.quadraticCurveTo (pts[0], pts[1], pts[2], pts[3]);
197 this.ctx.bezeierCurveTo (pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
200 this.ctx.closePath ();
205 return pi.getWindingRule ();
206 }, "java.awt.Shape");
207 Clazz.overrideMethod (c$, "fill",
209 if (this.doShape (s) == 0) {
210 this.ctx.fill("evenodd");
211 } else this.ctx.fill ();
212 }, "java.awt.Shape");
213 Clazz.defineMethod (c$, "drawImage",
214 function (img, x, y, observer) {
216 var imgNode = this.getImageNode (img);
217 if (imgNode != null) this.ctx.drawImage (imgNode, x, y, img.getWidth (observer), img.getHeight (observer));
218 if (observer != null) this.observe (img, observer, imgNode != null);
220 }, "java.awt.Image,~N,~N,java.awt.image.ImageObserver");
221 Clazz.defineMethod (c$, "observe",
222 function (img, observer, isOK) {
223 observer.imageUpdate (img, (isOK ? 0 : 192), -1, -1, -1, -1);
224 }, "java.awt.Image,java.awt.image.ImageObserver,~B");
225 Clazz.defineMethod (c$, "drawImage",
226 function (img, x, y, width, height, observer) {
228 var imgNode = this.getImageNode (img);
229 if (imgNode != null) this.ctx.drawImage (imgNode, x, y, width, height);
230 if (observer != null) this.observe (img, observer, imgNode != null);
232 }, "java.awt.Image,~N,~N,~N,~N,java.awt.image.ImageObserver");
233 Clazz.defineMethod (c$, "drawImage",
234 function (img, x, y, bgcolor, observer) {
235 swingjs.JSToolkit.notImplemented (null);
236 return this.drawImage (img, x, y, null);
237 }, "java.awt.Image,~N,~N,java.awt.Color,java.awt.image.ImageObserver");
238 Clazz.defineMethod (c$, "drawImage",
239 function (img, x, y, width, height, bgcolor, observer) {
240 swingjs.JSToolkit.notImplemented (null);
241 return this.drawImage (img, x, y, width, height, null);
242 }, "java.awt.Image,~N,~N,~N,~N,java.awt.Color,java.awt.image.ImageObserver");
243 Clazz.defineMethod (c$, "drawImage",
244 function (img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer) {
246 var imgNode = this.getImageNode (img);
247 if (imgNode != null) swingjs.api.HTML5CanvasContext2D.stretchImage (this.ctx, imgNode, sx1, sy1, sx2 - sx1, sy2 - sy1, dx1, dy1, dx2 - dx1, dy2 - dy1);
248 if (observer != null) this.observe (img, observer, imgNode != null);
250 }, "java.awt.Image,~N,~N,~N,~N,~N,~N,~N,~N,java.awt.image.ImageObserver");
251 Clazz.defineMethod (c$, "getImageNode",
255 imgNode = img._imgNode || img._canvas;
256 }if (imgNode == null) imgNode = swingjs.JSToolkit.getCompositor ().createImageNode (img);
258 }, "java.awt.Image");
259 Clazz.defineMethod (c$, "drawImage",
260 function (img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer) {
261 swingjs.JSToolkit.notImplemented (null);
262 return this.drawImage (img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
263 }, "java.awt.Image,~N,~N,~N,~N,~N,~N,~N,~N,java.awt.Color,java.awt.image.ImageObserver");
264 Clazz.defineMethod (c$, "drawImage",
265 function (img, xform, obs) {
266 swingjs.JSToolkit.notImplemented (null);
268 }, "java.awt.Image,java.awt.geom.AffineTransform,java.awt.image.ImageObserver");
269 Clazz.overrideMethod (c$, "drawRenderedImage",
270 function (img, xform) {
271 swingjs.JSToolkit.notImplemented (null);
272 }, "java.awt.image.RenderedImage,java.awt.geom.AffineTransform");
273 Clazz.overrideMethod (c$, "drawRenderableImage",
274 function (img, xform) {
275 swingjs.JSToolkit.notImplemented (null);
276 }, "java.awt.image.renderable.RenderableImage,java.awt.geom.AffineTransform");
277 Clazz.overrideMethod (c$, "hit",
278 function (rect, s, onStroke) {
279 swingjs.JSToolkit.notImplemented (null);
281 }, "java.awt.Rectangle,java.awt.Shape,~B");
282 Clazz.overrideMethod (c$, "setPaint",
284 swingjs.JSToolkit.notImplemented (null);
285 }, "java.awt.Paint");
286 Clazz.overrideMethod (c$, "setStroke",
288 if (!(Clazz.instanceOf (s, java.awt.BasicStroke))) return;
290 var dash = b.getDashArray ();
291 var idash = Clazz.newIntArray (dash == null ? 0 : dash.length, 0);
292 for (var i = idash.length; --i >= 0; ) idash[i] = Clazz.floatToInt (dash[i]);
294 this.ctx.setLineDash (idash);
295 this.setLineWidth (b.getLineWidth ());
299 switch (b.getEndCap ()) {
310 switch (b.getLineJoin ()) {
316 miterLimit = b.getMiterLimit ();
322 this.ctx.lineCap = lineCap; this.ctx.lineJoin = lineJoin; if
323 (miterLimit >= 0) this.ctx.miterLimit = miterLimit;
324 }}, "java.awt.Stroke");
325 Clazz.overrideMethod (c$, "setRenderingHint",
326 function (hintKey, hintValue) {
327 this.hints.put (hintKey, hintValue);
328 }, "java.awt.RenderingHints.Key,~O");
329 Clazz.overrideMethod (c$, "getRenderingHint",
331 return this.hints.get (hintKey);
332 }, "java.awt.RenderingHints.Key");
333 Clazz.overrideMethod (c$, "setRenderingHints",
335 this.hints = new java.awt.RenderingHints (hints);
337 Clazz.overrideMethod (c$, "addRenderingHints",
339 for (var e, $e = hints.entrySet ().iterator (); $e.hasNext () && ((e = $e.next ()) || true);) this.hints.put (e.getKey (), e.getValue ());
342 Clazz.overrideMethod (c$, "getRenderingHints",
346 Clazz.defineMethod (c$, "translate",
348 this.ctx.translate (x, y);
350 Clazz.overrideMethod (c$, "scale",
352 this.ctx.scale (sx, sy);
354 Clazz.overrideMethod (c$, "setBackground",
356 this.background (color);
357 }, "java.awt.Color");
358 Clazz.overrideMethod (c$, "getBackground",
360 return this.backgroundColor;
362 Clazz.overrideMethod (c$, "createSwingJS",
364 return this.clone ();
366 Clazz.overrideMethod (c$, "clone",
369 return this.clone0 ();
371 Clazz.overrideMethod (c$, "dispose",
373 if (this.compositeState >= 0) this.setComposite (null);
376 Clazz.overrideMethod (c$, "getColor",
378 return this.foregroundColor;
380 Clazz.overrideMethod (c$, "setColor",
382 this.foregroundColor = c;
383 this.setGraphicsColor (c);
384 }, "java.awt.Color");
385 Clazz.overrideMethod (c$, "getFont",
389 Clazz.defineMethod (c$, "getFontMetrics",
391 return java.awt.Toolkit.getDefaultToolkit ().getFontMetrics (f);
393 Clazz.overrideMethod (c$, "clipRect",
394 function (x, y, width, height) {
395 this.ctx.beginPath ();
396 this.ctx.rect (x, y, width, height);
397 this.currentClip = new java.awt.Rectangle (x, y, width, height);
400 Clazz.defineMethod (c$, "setClip",
401 function (x, y, width, height) {
402 this.currentClip = new java.awt.Rectangle (x, y, width, height);
404 if (arguments.length == 1) { setClip1(x); return; }
405 }this.ctx.beginPath ();
406 this.ctx.rect (x, y, width, height);
407 this.currentClip = new java.awt.Rectangle (x, y, width, height);
410 Clazz.defineMethod (c$, "setClip1",
412 this.ctx.beginPath ();
415 }, "java.awt.Shape");
416 Clazz.overrideMethod (c$, "clearRect",
417 function (x, y, width, height) {
418 this.ctx.clearRect (x, y, width, height);
420 Clazz.overrideMethod (c$, "drawPolyline",
421 function (xPoints, yPoints, nPoints) {
422 if (nPoints < 2) return;
423 this.ctx.moveTo (xPoints[0], yPoints[0]);
424 for (var i = 1; i < nPoints; i++) {
425 this.ctx.lineTo (xPoints[i], yPoints[i]);
428 Clazz.overrideMethod (c$, "copyArea",
429 function (x, y, width, height, dx, dy) {
430 swingjs.JSToolkit.notImplemented (null);
431 }, "~N,~N,~N,~N,~N,~N");
432 Clazz.overrideMethod (c$, "drawRoundRect",
433 function (x, y, width, height, arcWidth, arcHeight) {
434 swingjs.JSToolkit.notImplemented (null);
435 this.drawRect (x, y, width, height);
436 }, "~N,~N,~N,~N,~N,~N");
437 Clazz.overrideMethod (c$, "fillRoundRect",
438 function (x, y, width, height, arcWidth, arcHeight) {
439 swingjs.JSToolkit.notImplemented (null);
440 this.fillRect (x, y, width, height);
441 }, "~N,~N,~N,~N,~N,~N");
442 Clazz.overrideMethod (c$, "drawOval",
443 function (x, y, width, height) {
444 swingjs.JSToolkit.notImplemented (null);
446 Clazz.overrideMethod (c$, "fillOval",
447 function (x, y, width, height) {
448 swingjs.JSToolkit.notImplemented (null);
450 Clazz.overrideMethod (c$, "getClip",
452 swingjs.JSToolkit.notImplemented (null);
455 Clazz.defineMethod (c$, "drawString",
457 this.ctx.fillText (s, x, y);
459 Clazz.defineMethod (c$, "drawStringUnique",
461 this.ctx.fillText (s, x, y);
463 Clazz.overrideMethod (c$, "drawStringTrans",
464 function (str, x, y) {
465 swingjs.JSToolkit.notImplemented (null);
467 Clazz.defineMethod (c$, "drawString",
468 function (iterator, x, y) {
469 swingjs.JSToolkit.notImplemented (null);
470 }, "java.text.AttributedCharacterIterator,~N,~N");
471 Clazz.overrideMethod (c$, "drawStringAttrTrans",
472 function (iterator, x, y) {
473 swingjs.JSToolkit.notImplemented (null);
474 }, "java.text.AttributedCharacterIterator,~N,~N");
475 Clazz.overrideMethod (c$, "translateTrans",
477 swingjs.JSToolkit.notImplemented (null);
479 Clazz.defineMethod (c$, "rotate",
481 swingjs.JSToolkit.notImplemented (null);
483 Clazz.defineMethod (c$, "rotate",
484 function (theta, x, y) {
485 swingjs.JSToolkit.notImplemented (null);
487 Clazz.overrideMethod (c$, "shear",
488 function (shx, shy) {
489 swingjs.JSToolkit.notImplemented (null);
491 Clazz.overrideMethod (c$, "transform",
493 swingjs.JSToolkit.notImplemented (null);
494 }, "java.awt.geom.AffineTransform");
495 Clazz.overrideMethod (c$, "setTransform",
497 swingjs.JSToolkit.notImplemented (null);
498 }, "java.awt.geom.AffineTransform");
499 Clazz.overrideMethod (c$, "getTransform",
501 swingjs.JSToolkit.notImplemented (null);
504 Clazz.defineMethod (c$, "cloneTransform",
506 swingjs.JSToolkit.notImplemented (null);
509 Clazz.overrideMethod (c$, "getPaint",
511 swingjs.JSToolkit.notImplemented (null);
514 Clazz.overrideMethod (c$, "getStroke",
516 swingjs.JSToolkit.notImplemented (null);
519 Clazz.overrideMethod (c$, "getFontRenderContext",
521 swingjs.JSToolkit.notImplemented (null);
524 Clazz.overrideMethod (c$, "setPaintMode",
526 swingjs.JSToolkit.notImplemented (null);
528 Clazz.overrideMethod (c$, "setXORMode",
530 swingjs.JSToolkit.notImplemented (null);
531 }, "java.awt.Color");
532 Clazz.defineMethod (c$, "getClipBounds",
536 if (arguments.length == 1) r = arguments[0];
537 }var clipRect = this.getClipBoundsImpl ();
543 r.width = clipRect.width;
544 r.height = clipRect.height;
547 Clazz.defineMethod (c$, "getClipBoundsImpl",
549 if (this.currentClip == null) {
550 this.currentClip = new java.awt.Rectangle (0, 0, this.windowWidth, this.windowHeight);
551 }return this.currentClip;
553 Clazz.overrideMethod (c$, "setComposite",
556 var isValid = (comp == null || (Clazz.instanceOf (comp, java.awt.AlphaComposite)) && (newRule = (comp).getRule ()) != this.compositeState);
557 if (!isValid) return;
558 if (swingjs.JSToolkit.setGraphicsCompositeAlpha (this, newRule)) this.compositeState = newRule;
559 }, "java.awt.Composite");
560 Clazz.defineMethod (c$, "drawImage",
561 function (img, op, x, y) {
562 swingjs.JSToolkit.drawImageOp (this, img, op, x, y);
563 }, "java.awt.image.BufferedImage,java.awt.image.BufferedImageOp,~N,~N");
564 Clazz.defineMethod (c$, "setAlpha",
567 this.ctx.globalAlpha = f;
569 Clazz.defineStatics (c$,