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) {
222 var imgNode = this.getImageNode (img);
223 if (imgNode != null) this.ctx.drawImage (imgNode, x, y, img.getWidth (observer), img.getHeight (observer));
224 if (observer != null) this.observe (img, observer, imgNode != null);
226 }, "java.awt.Image,~N,~N,java.awt.image.ImageObserver");
227 Clazz.defineMethod (c$, "observe",
228 function (img, observer, isOK) {
229 observer.imageUpdate (img, (isOK ? 0 : 192), -1, -1, -1, -1);
230 }, "java.awt.Image,java.awt.image.ImageObserver,~B");
231 Clazz.defineMethod (c$, "drawImage",
232 function (img, x, y, width, height, observer) {
234 var imgNode = this.getImageNode (img);
235 if (imgNode != null) this.ctx.drawImage (imgNode, x, y, width, height);
236 if (observer != null) this.observe (img, observer, imgNode != null);
238 }, "java.awt.Image,~N,~N,~N,~N,java.awt.image.ImageObserver");
239 Clazz.defineMethod (c$, "drawImage",
240 function (img, x, y, bgcolor, observer) {
241 swingjs.JSToolkit.notImplemented (null);
242 return this.drawImage (img, x, y, null);
243 }, "java.awt.Image,~N,~N,java.awt.Color,java.awt.image.ImageObserver");
244 Clazz.defineMethod (c$, "drawImage",
245 function (img, x, y, width, height, bgcolor, observer) {
246 swingjs.JSToolkit.notImplemented (null);
247 return this.drawImage (img, x, y, width, height, null);
248 }, "java.awt.Image,~N,~N,~N,~N,java.awt.Color,java.awt.image.ImageObserver");
249 Clazz.defineMethod (c$, "drawImage",
250 function (img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer) {
252 var imgNode = this.getImageNode (img);
253 if (imgNode != null) swingjs.api.HTML5CanvasContext2D.stretchImage (this.ctx, imgNode, sx1, sy1, sx2 - sx1, sy2 - sy1, dx1, dy1, dx2 - dx1, dy2 - dy1);
254 if (observer != null) this.observe (img, observer, imgNode != null);
256 }, "java.awt.Image,~N,~N,~N,~N,~N,~N,~N,~N,java.awt.image.ImageObserver");
257 Clazz.defineMethod (c$, "getImageNode",
261 imgNode = img._imgNode || img._canvas;
263 }if (imgNode == null) imgNode = swingjs.JSToolkit.getCompositor ().createImageNode (img);
265 }, "java.awt.Image");
266 Clazz.defineMethod (c$, "drawImage",
267 function (img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer) {
268 swingjs.JSToolkit.notImplemented (null);
269 return this.drawImage (img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
270 }, "java.awt.Image,~N,~N,~N,~N,~N,~N,~N,~N,java.awt.Color,java.awt.image.ImageObserver");
271 Clazz.defineMethod (c$, "drawImage",
272 function (img, xform, obs) {
273 swingjs.JSToolkit.notImplemented (null);
275 }, "java.awt.Image,java.awt.geom.AffineTransform,java.awt.image.ImageObserver");
276 Clazz.overrideMethod (c$, "drawRenderedImage",
277 function (img, xform) {
278 swingjs.JSToolkit.notImplemented (null);
279 }, "java.awt.image.RenderedImage,java.awt.geom.AffineTransform");
280 Clazz.overrideMethod (c$, "drawRenderableImage",
281 function (img, xform) {
282 swingjs.JSToolkit.notImplemented (null);
283 }, "java.awt.image.renderable.RenderableImage,java.awt.geom.AffineTransform");
284 Clazz.overrideMethod (c$, "hit",
285 function (rect, s, onStroke) {
286 swingjs.JSToolkit.notImplemented (null);
288 }, "java.awt.Rectangle,java.awt.Shape,~B");
289 Clazz.overrideMethod (c$, "setPaint",
291 swingjs.JSToolkit.notImplemented (null);
292 }, "java.awt.Paint");
293 Clazz.overrideMethod (c$, "setStroke",
295 if (!(Clazz.instanceOf (s, java.awt.BasicStroke))) return;
297 var dash = b.getDashArray ();
298 var idash = Clazz.newIntArray (dash == null ? 0 : dash.length, 0);
299 for (var i = idash.length; --i >= 0; ) idash[i] = Clazz.floatToInt (dash[i]);
301 this.ctx.setLineDash (idash);
302 this.setLineWidth (b.getLineWidth ());
306 switch (b.getEndCap ()) {
317 switch (b.getLineJoin ()) {
323 miterLimit = b.getMiterLimit ();
329 this.ctx.lineCap = lineCap; this.ctx.lineJoin = lineJoin; if
330 (miterLimit >= 0) this.ctx.miterLimit = miterLimit;
331 }}, "java.awt.Stroke");
332 Clazz.overrideMethod (c$, "setRenderingHint",
333 function (hintKey, hintValue) {
334 this.hints.put (hintKey, hintValue);
335 }, "java.awt.RenderingHints.Key,~O");
336 Clazz.overrideMethod (c$, "getRenderingHint",
338 return this.hints.get (hintKey);
339 }, "java.awt.RenderingHints.Key");
340 Clazz.overrideMethod (c$, "setRenderingHints",
342 this.hints = new java.awt.RenderingHints (hints);
344 Clazz.overrideMethod (c$, "addRenderingHints",
346 for (var e, $e = hints.entrySet ().iterator (); $e.hasNext () && ((e = $e.next ()) || true);) this.hints.put (e.getKey (), e.getValue ());
349 Clazz.overrideMethod (c$, "getRenderingHints",
353 Clazz.defineMethod (c$, "translate",
355 this.ctx.translate (x, y);
357 Clazz.overrideMethod (c$, "scale",
359 this.ctx.scale (sx, sy);
361 Clazz.overrideMethod (c$, "setBackground",
363 this.background (color);
364 }, "java.awt.Color");
365 Clazz.overrideMethod (c$, "getBackground",
367 return this.backgroundColor;
369 Clazz.overrideMethod (c$, "createSwingJS",
371 return this.clone ();
373 Clazz.overrideMethod (c$, "clone",
376 return this.clone0 ();
378 Clazz.overrideMethod (c$, "dispose",
380 if (this.compositeState >= 0) this.setComposite (null);
383 Clazz.overrideMethod (c$, "getColor",
385 return this.foregroundColor;
387 Clazz.overrideMethod (c$, "setColor",
389 this.foregroundColor = c;
390 this.setGraphicsColor (c);
391 }, "java.awt.Color");
392 Clazz.overrideMethod (c$, "getFont",
396 Clazz.defineMethod (c$, "getFontMetrics",
398 return java.awt.Toolkit.getDefaultToolkit ().getFontMetrics (f);
400 Clazz.overrideMethod (c$, "clipRect",
401 function (x, y, width, height) {
402 this.ctx.beginPath ();
403 this.ctx.rect (x, y, width, height);
404 this.currentClip = new java.awt.Rectangle (x, y, width, height);
407 Clazz.defineMethod (c$, "setClip",
408 function (x, y, width, height) {
409 this.currentClip = new java.awt.Rectangle (x, y, width, height);
411 if (arguments.length == 1) { setClip1(x); return; }
412 }this.ctx.beginPath ();
413 this.ctx.rect (x, y, width, height);
414 this.currentClip = new java.awt.Rectangle (x, y, width, height);
417 Clazz.defineMethod (c$, "setClip1",
419 this.ctx.beginPath ();
422 }, "java.awt.Shape");
423 Clazz.overrideMethod (c$, "clearRect",
424 function (x, y, width, height) {
425 this.ctx.clearRect (x, y, width, height);
427 Clazz.overrideMethod (c$, "drawPolyline",
428 function (xPoints, yPoints, nPoints) {
429 if (nPoints < 2) return;
430 this.ctx.moveTo (xPoints[0], yPoints[0]);
431 for (var i = 1; i < nPoints; i++) {
432 this.ctx.lineTo (xPoints[i], yPoints[i]);
435 Clazz.overrideMethod (c$, "copyArea",
436 function (x, y, width, height, dx, dy) {
437 swingjs.JSToolkit.notImplemented (null);
438 }, "~N,~N,~N,~N,~N,~N");
439 Clazz.overrideMethod (c$, "drawRoundRect",
440 function (x, y, width, height, arcWidth, arcHeight) {
441 swingjs.JSToolkit.notImplemented (null);
442 this.drawRect (x, y, width, height);
443 }, "~N,~N,~N,~N,~N,~N");
444 Clazz.overrideMethod (c$, "fillRoundRect",
445 function (x, y, width, height, arcWidth, arcHeight) {
446 swingjs.JSToolkit.notImplemented (null);
447 this.fillRect (x, y, width, height);
448 }, "~N,~N,~N,~N,~N,~N");
449 Clazz.overrideMethod (c$, "drawOval",
450 function (x, y, width, height) {
451 swingjs.JSToolkit.notImplemented (null);
453 Clazz.overrideMethod (c$, "fillOval",
454 function (x, y, width, height) {
455 swingjs.JSToolkit.notImplemented (null);
457 Clazz.overrideMethod (c$, "getClip",
459 swingjs.JSToolkit.notImplemented (null);
462 Clazz.overrideMethod (c$, "drawStringTrans",
463 function (str, x, y) {
464 swingjs.JSToolkit.notImplemented (null);
466 Clazz.defineMethod (c$, "drawString",
467 function (iterator, x, y) {
468 swingjs.JSToolkit.notImplemented (null);
469 }, "java.text.AttributedCharacterIterator,~N,~N");
470 Clazz.overrideMethod (c$, "drawStringAttrTrans",
471 function (iterator, x, y) {
472 swingjs.JSToolkit.notImplemented (null);
473 }, "java.text.AttributedCharacterIterator,~N,~N");
474 Clazz.overrideMethod (c$, "translateTrans",
476 swingjs.JSToolkit.notImplemented (null);
478 Clazz.defineMethod (c$, "rotate",
480 swingjs.JSToolkit.notImplemented (null);
482 Clazz.defineMethod (c$, "rotate",
483 function (theta, x, y) {
484 swingjs.JSToolkit.notImplemented (null);
486 Clazz.overrideMethod (c$, "shear",
487 function (shx, shy) {
488 swingjs.JSToolkit.notImplemented (null);
490 Clazz.overrideMethod (c$, "transform",
492 swingjs.JSToolkit.notImplemented (null);
493 }, "java.awt.geom.AffineTransform");
494 Clazz.overrideMethod (c$, "setTransform",
496 swingjs.JSToolkit.notImplemented (null);
497 }, "java.awt.geom.AffineTransform");
498 Clazz.overrideMethod (c$, "getTransform",
500 swingjs.JSToolkit.notImplemented (null);
503 Clazz.defineMethod (c$, "cloneTransform",
505 swingjs.JSToolkit.notImplemented (null);
508 Clazz.overrideMethod (c$, "getPaint",
510 swingjs.JSToolkit.notImplemented (null);
513 Clazz.overrideMethod (c$, "getStroke",
515 swingjs.JSToolkit.notImplemented (null);
518 Clazz.overrideMethod (c$, "getFontRenderContext",
520 swingjs.JSToolkit.notImplemented (null);
523 Clazz.overrideMethod (c$, "setPaintMode",
525 swingjs.JSToolkit.notImplemented (null);
527 Clazz.overrideMethod (c$, "setXORMode",
529 swingjs.JSToolkit.notImplemented (null);
530 }, "java.awt.Color");
531 Clazz.defineMethod (c$, "getClipBounds",
535 if (arguments.length == 1) r = arguments[0];
536 }var clipRect = this.getClipBoundsImpl ();
542 r.width = clipRect.width;
543 r.height = clipRect.height;
546 Clazz.defineMethod (c$, "getClipBoundsImpl",
548 if (this.currentClip == null) {
549 this.currentClip = new java.awt.Rectangle (0, 0, this.windowWidth, this.windowHeight);
550 }return this.currentClip;
552 Clazz.overrideMethod (c$, "setComposite",
555 var isValid = (comp == null || (Clazz.instanceOf (comp, java.awt.AlphaComposite)) && (newRule = (comp).getRule ()) != this.compositeState);
556 if (!isValid) return;
557 if (swingjs.JSToolkit.setGraphicsCompositeAlpha (this, newRule)) this.compositeState = newRule;
558 }, "java.awt.Composite");
559 Clazz.defineMethod (c$, "drawImage",
560 function (img, op, x, y) {
561 swingjs.JSToolkit.drawImageOp (this, img, op, x, y);
562 }, "java.awt.image.BufferedImage,java.awt.image.BufferedImageOp,~N,~N");
563 Clazz.defineMethod (c$, "setAlpha",
566 this.ctx.globalAlpha = f;
568 Clazz.defineStatics (c$,