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$, "drawString",
102 this.ctx.fillText (s, x, y);
104 Clazz.defineMethod (c$, "background",
106 this.backgroundColor = bgcolor;
107 if (bgcolor == null) {
108 if (!this.isShifted) this.ctx.translate (-0.5, -0.5);
109 this.isShifted = true;
111 }this.ctx.clearRect (0, 0, this.windowWidth, this.windowHeight);
112 this.setGraphicsColor (bgcolor);
113 this.fillRect (0, 0, this.windowWidth, this.windowHeight);
114 }, "java.awt.Color");
115 Clazz.defineMethod (c$, "fillCircle",
116 function (x, y, diameter) {
117 var r = diameter / 2;
118 this.ctx.beginPath ();
119 this.ctx.arc (x + r, y + r, r, 0, 6.283185307179586, false);
122 Clazz.defineMethod (c$, "fillPolygon",
123 function (ayPoints, axPoints, nPoints) {
124 this.doPoly (ayPoints, axPoints, nPoints, true);
126 Clazz.overrideMethod (c$, "fillRect",
127 function (x, y, width, height) {
128 this.ctx.fillRect (x, y, width, height);
130 Clazz.defineMethod (c$, "setGraphicsColor",
132 var s = swingjs.JSToolkit.getCSSColor (c);
134 this.ctx.fillStyle = s; this.ctx.strokeStyle = s;
135 }}, "java.awt.Color");
136 Clazz.overrideMethod (c$, "setFont",
139 if (this.ctx == null) return;
140 var s = swingjs.JSToolkit.getCanvasFont (font);
143 }}, "java.awt.Font");
144 Clazz.defineMethod (c$, "setStrokeBold",
146 this.setLineWidth (tf ? 2. : 1.);
148 Clazz.defineMethod (c$, "setLineWidth",
151 this.ctx.lineWidth = d;
153 Clazz.defineMethod (c$, "setWindowParameters",
154 function (width, height) {
155 this.windowWidth = width;
156 this.windowHeight = height;
158 Clazz.defineMethod (c$, "canDoLineTo",
162 Clazz.defineMethod (c$, "doStroke",
164 this.inPath = isBegin;
166 this.ctx.beginPath ();
170 Clazz.defineMethod (c$, "lineTo",
172 this.ctx.lineTo (x2, y2);
174 Clazz.overrideMethod (c$, "clip",
178 }, "java.awt.Shape");
179 Clazz.overrideMethod (c$, "draw",
183 }, "java.awt.Shape");
184 Clazz.defineMethod (c$, "doShape",
186 this.ctx.beginPath ();
187 var pts = Clazz.newDoubleArray (6, 0);
188 var pi = s.getPathIterator (null);
189 while (!pi.isDone ()) {
190 switch (pi.currentSegment (pts)) {
192 this.ctx.moveTo (pts[0], pts[1]);
195 this.ctx.lineTo (pts[0], pts[1]);
198 this.ctx.quadraticCurveTo (pts[0], pts[1], pts[2], pts[3]);
201 this.ctx.bezeierCurveTo (pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
204 this.ctx.closePath ();
209 return pi.getWindingRule ();
210 }, "java.awt.Shape");
211 Clazz.overrideMethod (c$, "fill",
213 if (this.doShape (s) == 0) {
214 this.ctx.fill("evenodd");
215 } else this.ctx.fill ();
216 }, "java.awt.Shape");
217 Clazz.defineMethod (c$, "drawImage",
218 function (img, x, y, observer) {
220 var imgNode = this.getImageNode (img);
221 if (imgNode != null) this.ctx.drawImage (imgNode, x, y, img.getWidth (observer), img.getHeight (observer));
222 if (observer != null) this.observe (img, observer, imgNode != null);
224 }, "java.awt.Image,~N,~N,java.awt.image.ImageObserver");
225 Clazz.defineMethod (c$, "observe",
226 function (img, observer, isOK) {
227 observer.imageUpdate (img, (isOK ? 0 : 192), -1, -1, -1, -1);
228 }, "java.awt.Image,java.awt.image.ImageObserver,~B");
229 Clazz.defineMethod (c$, "drawImage",
230 function (img, x, y, width, height, observer) {
232 var imgNode = this.getImageNode (img);
233 if (imgNode != null) this.ctx.drawImage (imgNode, x, y, width, height);
234 if (observer != null) this.observe (img, observer, imgNode != null);
236 }, "java.awt.Image,~N,~N,~N,~N,java.awt.image.ImageObserver");
237 Clazz.defineMethod (c$, "drawImage",
238 function (img, x, y, bgcolor, observer) {
239 swingjs.JSToolkit.notImplemented (null);
240 return this.drawImage (img, x, y, null);
241 }, "java.awt.Image,~N,~N,java.awt.Color,java.awt.image.ImageObserver");
242 Clazz.defineMethod (c$, "drawImage",
243 function (img, x, y, width, height, bgcolor, observer) {
244 swingjs.JSToolkit.notImplemented (null);
245 return this.drawImage (img, x, y, width, height, null);
246 }, "java.awt.Image,~N,~N,~N,~N,java.awt.Color,java.awt.image.ImageObserver");
247 Clazz.defineMethod (c$, "drawImage",
248 function (img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer) {
250 var imgNode = this.getImageNode (img);
251 if (imgNode != null) swingjs.api.HTML5CanvasContext2D.stretchImage (this.ctx, imgNode, sx1, sy1, sx2 - sx1, sy2 - sy1, dx1, dy1, dx2 - dx1, dy2 - dy1);
252 if (observer != null) this.observe (img, observer, imgNode != null);
254 }, "java.awt.Image,~N,~N,~N,~N,~N,~N,~N,~N,java.awt.image.ImageObserver");
255 Clazz.defineMethod (c$, "getImageNode",
259 imgNode = img._imgNode || img._canvas;
260 }if (imgNode == null) imgNode = swingjs.JSToolkit.getCompositor ().createImageNode (img);
262 }, "java.awt.Image");
263 Clazz.defineMethod (c$, "drawImage",
264 function (img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer) {
265 swingjs.JSToolkit.notImplemented (null);
266 return this.drawImage (img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
267 }, "java.awt.Image,~N,~N,~N,~N,~N,~N,~N,~N,java.awt.Color,java.awt.image.ImageObserver");
268 Clazz.defineMethod (c$, "drawImage",
269 function (img, xform, obs) {
270 swingjs.JSToolkit.notImplemented (null);
272 }, "java.awt.Image,java.awt.geom.AffineTransform,java.awt.image.ImageObserver");
273 Clazz.overrideMethod (c$, "drawRenderedImage",
274 function (img, xform) {
275 swingjs.JSToolkit.notImplemented (null);
276 }, "java.awt.image.RenderedImage,java.awt.geom.AffineTransform");
277 Clazz.overrideMethod (c$, "drawRenderableImage",
278 function (img, xform) {
279 swingjs.JSToolkit.notImplemented (null);
280 }, "java.awt.image.renderable.RenderableImage,java.awt.geom.AffineTransform");
281 Clazz.overrideMethod (c$, "hit",
282 function (rect, s, onStroke) {
283 swingjs.JSToolkit.notImplemented (null);
285 }, "java.awt.Rectangle,java.awt.Shape,~B");
286 Clazz.overrideMethod (c$, "setPaint",
288 swingjs.JSToolkit.notImplemented (null);
289 }, "java.awt.Paint");
290 Clazz.overrideMethod (c$, "setStroke",
292 if (!(Clazz.instanceOf (s, java.awt.BasicStroke))) return;
294 var dash = b.getDashArray ();
295 var idash = Clazz.newIntArray (dash == null ? 0 : dash.length, 0);
296 for (var i = idash.length; --i >= 0; ) idash[i] = Clazz.floatToInt (dash[i]);
298 this.ctx.setLineDash (idash);
299 this.setLineWidth (b.getLineWidth ());
303 switch (b.getEndCap ()) {
314 switch (b.getLineJoin ()) {
320 miterLimit = b.getMiterLimit ();
326 this.ctx.lineCap = lineCap; this.ctx.lineJoin = lineJoin; if
327 (miterLimit >= 0) this.ctx.miterLimit = miterLimit;
328 }}, "java.awt.Stroke");
329 Clazz.overrideMethod (c$, "setRenderingHint",
330 function (hintKey, hintValue) {
331 this.hints.put (hintKey, hintValue);
332 }, "java.awt.RenderingHints.Key,~O");
333 Clazz.overrideMethod (c$, "getRenderingHint",
335 return this.hints.get (hintKey);
336 }, "java.awt.RenderingHints.Key");
337 Clazz.overrideMethod (c$, "setRenderingHints",
339 this.hints = new java.awt.RenderingHints (hints);
341 Clazz.overrideMethod (c$, "addRenderingHints",
343 for (var e, $e = hints.entrySet ().iterator (); $e.hasNext () && ((e = $e.next ()) || true);) this.hints.put (e.getKey (), e.getValue ());
346 Clazz.overrideMethod (c$, "getRenderingHints",
350 Clazz.defineMethod (c$, "translate",
352 this.ctx.translate (x, y);
354 Clazz.overrideMethod (c$, "scale",
356 this.ctx.scale (sx, sy);
358 Clazz.overrideMethod (c$, "setBackground",
360 this.background (color);
361 }, "java.awt.Color");
362 Clazz.overrideMethod (c$, "getBackground",
364 return this.backgroundColor;
366 Clazz.overrideMethod (c$, "createSwingJS",
368 return this.clone ();
370 Clazz.overrideMethod (c$, "clone",
373 return this.clone0 ();
375 Clazz.overrideMethod (c$, "dispose",
377 if (this.compositeState >= 0) this.setComposite (null);
380 Clazz.overrideMethod (c$, "getColor",
382 return this.foregroundColor;
384 Clazz.overrideMethod (c$, "setColor",
386 this.foregroundColor = c;
387 this.setGraphicsColor (c);
388 }, "java.awt.Color");
389 Clazz.overrideMethod (c$, "getFont",
393 Clazz.defineMethod (c$, "getFontMetrics",
395 return java.awt.Toolkit.getDefaultToolkit ().getFontMetrics (f);
397 Clazz.overrideMethod (c$, "clipRect",
398 function (x, y, width, height) {
399 this.ctx.beginPath ();
400 this.ctx.rect (x, y, width, height);
401 this.currentClip = new java.awt.Rectangle (x, y, width, height);
404 Clazz.defineMethod (c$, "setClip",
405 function (x, y, width, height) {
406 this.currentClip = new java.awt.Rectangle (x, y, width, height);
408 if (arguments.length == 1) { setClip1(x); return; }
409 }this.ctx.beginPath ();
410 this.ctx.rect (x, y, width, height);
411 this.currentClip = new java.awt.Rectangle (x, y, width, height);
414 Clazz.defineMethod (c$, "setClip1",
416 this.ctx.beginPath ();
419 }, "java.awt.Shape");
420 Clazz.overrideMethod (c$, "clearRect",
421 function (x, y, width, height) {
422 this.ctx.clearRect (x, y, width, height);
424 Clazz.overrideMethod (c$, "drawPolyline",
425 function (xPoints, yPoints, nPoints) {
426 if (nPoints < 2) return;
427 this.ctx.moveTo (xPoints[0], yPoints[0]);
428 for (var i = 1; i < nPoints; i++) {
429 this.ctx.lineTo (xPoints[i], yPoints[i]);
432 Clazz.overrideMethod (c$, "copyArea",
433 function (x, y, width, height, dx, dy) {
434 swingjs.JSToolkit.notImplemented (null);
435 }, "~N,~N,~N,~N,~N,~N");
436 Clazz.overrideMethod (c$, "drawRoundRect",
437 function (x, y, width, height, arcWidth, arcHeight) {
438 swingjs.JSToolkit.notImplemented (null);
439 this.drawRect (x, y, width, height);
440 }, "~N,~N,~N,~N,~N,~N");
441 Clazz.overrideMethod (c$, "fillRoundRect",
442 function (x, y, width, height, arcWidth, arcHeight) {
443 swingjs.JSToolkit.notImplemented (null);
444 this.fillRect (x, y, width, height);
445 }, "~N,~N,~N,~N,~N,~N");
446 Clazz.overrideMethod (c$, "drawOval",
447 function (x, y, width, height) {
448 swingjs.JSToolkit.notImplemented (null);
450 Clazz.overrideMethod (c$, "fillOval",
451 function (x, y, width, height) {
452 swingjs.JSToolkit.notImplemented (null);
454 Clazz.overrideMethod (c$, "getClip",
456 swingjs.JSToolkit.notImplemented (null);
459 Clazz.overrideMethod (c$, "drawStringTrans",
460 function (str, x, y) {
461 swingjs.JSToolkit.notImplemented (null);
463 Clazz.defineMethod (c$, "drawString",
464 function (iterator, x, y) {
465 swingjs.JSToolkit.notImplemented (null);
466 }, "java.text.AttributedCharacterIterator,~N,~N");
467 Clazz.overrideMethod (c$, "drawStringAttrTrans",
468 function (iterator, x, y) {
469 swingjs.JSToolkit.notImplemented (null);
470 }, "java.text.AttributedCharacterIterator,~N,~N");
471 Clazz.overrideMethod (c$, "translateTrans",
473 swingjs.JSToolkit.notImplemented (null);
475 Clazz.defineMethod (c$, "rotate",
477 swingjs.JSToolkit.notImplemented (null);
479 Clazz.defineMethod (c$, "rotate",
480 function (theta, x, y) {
481 swingjs.JSToolkit.notImplemented (null);
483 Clazz.overrideMethod (c$, "shear",
484 function (shx, shy) {
485 swingjs.JSToolkit.notImplemented (null);
487 Clazz.overrideMethod (c$, "transform",
489 swingjs.JSToolkit.notImplemented (null);
490 }, "java.awt.geom.AffineTransform");
491 Clazz.overrideMethod (c$, "setTransform",
493 swingjs.JSToolkit.notImplemented (null);
494 }, "java.awt.geom.AffineTransform");
495 Clazz.overrideMethod (c$, "getTransform",
497 swingjs.JSToolkit.notImplemented (null);
500 Clazz.defineMethod (c$, "cloneTransform",
502 swingjs.JSToolkit.notImplemented (null);
505 Clazz.overrideMethod (c$, "getPaint",
507 swingjs.JSToolkit.notImplemented (null);
510 Clazz.overrideMethod (c$, "getStroke",
512 swingjs.JSToolkit.notImplemented (null);
515 Clazz.overrideMethod (c$, "getFontRenderContext",
517 swingjs.JSToolkit.notImplemented (null);
520 Clazz.overrideMethod (c$, "setPaintMode",
522 swingjs.JSToolkit.notImplemented (null);
524 Clazz.overrideMethod (c$, "setXORMode",
526 swingjs.JSToolkit.notImplemented (null);
527 }, "java.awt.Color");
528 Clazz.defineMethod (c$, "getClipBounds",
532 if (arguments.length == 1) r = arguments[0];
533 }var clipRect = this.getClipBoundsImpl ();
539 r.width = clipRect.width;
540 r.height = clipRect.height;
543 Clazz.defineMethod (c$, "getClipBoundsImpl",
545 if (this.currentClip == null) {
546 this.currentClip = new java.awt.Rectangle (0, 0, this.windowWidth, this.windowHeight);
547 }return this.currentClip;
549 Clazz.overrideMethod (c$, "setComposite",
552 var isValid = (comp == null || (Clazz.instanceOf (comp, java.awt.AlphaComposite)) && (newRule = (comp).getRule ()) != this.compositeState);
553 if (!isValid) return;
554 if (swingjs.JSToolkit.setGraphicsCompositeAlpha (this, newRule)) this.compositeState = newRule;
555 }, "java.awt.Composite");
556 Clazz.defineMethod (c$, "drawImage",
557 function (img, op, x, y) {
558 swingjs.JSToolkit.drawImageOp (this, img, op, x, y);
559 }, "java.awt.image.BufferedImage,java.awt.image.BufferedImageOp,~N,~N");
560 Clazz.defineMethod (c$, "setAlpha",
563 this.ctx.globalAlpha = f;
565 Clazz.defineStatics (c$,