1
4
5 import org.aswing.graphices.Brush;
6 import org.aswing.graphices.Pen;
7
8
12 class org.aswing.graphices.Graphics {
13
14 private var target_mc:MovieClip;
15 private var brush:Brush;
16
17
21 public function Graphics(target_mc:MovieClip){
22 this.target_mc = target_mc;
23 }
24
25 private function setTarget(target_mc:MovieClip):Void{
26 this.target_mc = target_mc;
27 }
28
29 private function dispose():Void{
30 target_mc = null;
31 }
32
33 private function startPen(p:Pen):Void{
34 p.setTo(target_mc);
35 }
36
37 private function endPen():Void{
38 target_mc.lineStyle();
39 target_mc.moveTo(0, 0);
40 }
41
42 private function startBrush(b:Brush):Void{
43 brush = b;
44 b.beginFill(target_mc);
45 }
46
47 private function endBrush():Void{
48 brush.endFill(target_mc);
49 target_mc.moveTo(0, 0);
50 }
51
52
53
56 private function clear():Void {
57 if(target_mc!=undefined) target_mc.clear();
58 }
59
60
69 public function drawLine(p:Pen, x1:Number, y1:Number, x2:Number, y2:Number):Void {
70 startPen(p);
71 target_mc.moveTo(x1, y1);
72 target_mc.lineTo(x2, y2);
73
74 endPen();
75 }
76
77
84 public function drawPolygon(p:Pen, points:Array):Void{
85 startPen(p);
86 polygon(points);
87 endPen();
88 }
89
90
97 public function fillPolygon(b:Brush, points:Array):Void{
98 startBrush(b);
99 polygon(points);
100 endBrush();
101 }
102
103
109 public function fillPolygonRing(b:Brush, points1:Array, points2:Array):Void{
110 startBrush(b);
111 polygon(points1);
112 polygon(points2);
113 endBrush();
114 }
115
116
124 public function drawRectangle(pen:Pen, x:Number, y:Number, w:Number, h:Number):Void {
125 this.startPen(pen);
126 this.rectangle(x, y, w, h);
127 this.endPen();
128 }
129
130
138 public function fillRectangle(brush:Brush, x:Number, y:Number, width:Number, height:Number):Void{
139 startBrush(brush);
140 rectangle(x,y,width,height);
141 endBrush();
142 }
143
144
154 public function fillRectangleRing(brush:Brush, cx:Number, cy:Number, w1:Number, h1:Number, w2:Number, h2:Number):Void{
155 startBrush(brush);
156 rectangle(cx-w1/2, cy-h1/2, w1, h1);
157 rectangle(cx-w2/2, cy-h2/2, w2, h2);
158 endBrush();
159 }
160
161
170 public function fillRectangleRingWithThickness(brush:Brush, x:Number, y:Number, w:Number, h:Number, t:Number):Void{
171 startBrush(brush);
172 rectangle(x, y, w, h);
173 rectangle(x+t, y+t, w - t*2, h - t*2);
174 endBrush();
175 }
176
177
184 public function drawCircle(p:Pen, cx:Number, cy:Number, radius:Number):Void{
185 startPen(p);
186 circle(cx, cy, radius);
187 endPen();
188 }
189
190
197 public function fillCircle(b:Brush, cx:Number, cy:Number, radius:Number):Void{
198 startBrush(b);
199 circle(cx, cy, radius);
200 endBrush();
201 }
202
203
211 public function fillCircleRing(b:Brush, cx:Number, cy:Number, r1:Number, r2:Number):Void{
212 startBrush(b);
213 circle(cx, cy, r1);
214 circle(cx, cy, r2);
215 endBrush();
216 }
217
218
226 public function fillCircleRingWithThickness(b:Brush, cx:Number, cy:Number, r:Number, t:Number):Void{
227 startBrush(b);
228 circle(cx, cy, r);
229 r -= t;
230 circle(cx, cy, r);
231 endBrush();
232 }
233
234
242 public function drawEllipse(p:Pen, x:Number, y:Number, width:Number, height:Number):Void{
243 startPen(p);
244 ellipse(x, y, width, height);
245 endPen();
246 }
247
248
256 public function fillEllipse(b:Brush, x:Number, y:Number, width:Number, height:Number):Void{
257 startBrush(b);
258 ellipse(x, y, width, height);
259 endBrush();
260 }
261
262
272 public function fillEllipseRing(brush:Brush, cx:Number, cy:Number, w1:Number, h1:Number, w2:Number, h2:Number):Void{
273 startBrush(brush);
274 ellipse(cx-w1/2, cy-h1/2, w1, h1);
275 ellipse(cx-w2/2, cy-h2/2, w2, h2);
276 endBrush();
277 }
278
279
288 public function fillEllipseRingWithThickness(brush:Brush, x:Number, y:Number, w:Number, h:Number, t:Number):Void{
289 startBrush(brush);
290 ellipse(x, y, w, h);
291 ellipse(x+t, y+t, w-t*2, h-t*2);
292 endBrush();
293 }
294
295
307 public function drawRoundRect(pen:Pen, x:Number, y:Number, width:Number, height:Number, radius:Number, trR:Number, blR:Number, brR:Number):Void{
308 startPen(pen);
309 roundRect(x, y, width, height, radius, trR, blR, brR);
310 endPen();
311 }
312
313
325 public function fillRoundRect(brush:Brush, x:Number, y:Number, width:Number, height:Number, radius:Number, trR:Number, blR:Number, brR:Number):Void{
326 startBrush(brush);
327 roundRect(x,y,width,height,radius,trR,blR,brR);
328 endBrush();
329 }
330
331
343 public function fillRoundRectRing(brush:Brush,cx:Number,cy:Number,w1:Number,h1:Number,r1:Number, w2:Number, h2:Number, r2:Number):Void{
344 startBrush(brush);
345 roundRect(cx-w1/2, cy-h1/2, w1, h1, r1);
346 roundRect(cx-w2/2, cy-h2/2, w2, h2, r2);
347 endBrush();
348 }
349
350
361 public function fillRoundRectRingWithThickness(brush:Brush, x:Number, y:Number, w:Number, h:Number, r:Number, t:Number, ir:Number):Void{
362 startBrush(brush);
363 roundRect(x, y, w, h, r);
364 if(ir == undefined) ir = r - t;
365 roundRect(x+t, y+t, w-t*2, h-t*2, ir);
366 endBrush();
367 }
368
369 public function beginFill(brush:Brush):Void{
370 startBrush(brush);
371 }
372 public function endFill():Void{
373 endBrush();
374 target_mc.moveTo(0, 0);
375 }
376 public function beginDraw(pen:Pen):Void{
377 startPen(pen);
378 }
379 public function endDraw():Void{
380 endPen();
381 target_mc.moveTo(0, 0);
382 }
383 public function moveTo(x:Number, y:Number):Void{
384 target_mc.moveTo(x, y);
385 }
386 public function curveTo(controlX:Number, controlY:Number, anchorX:Number, anchorY:Number):Void{
387 target_mc.curveTo(controlX, controlY, anchorX, anchorY);
388 }
389 public function lineTo(x:Number, y:Number):Void{
390 target_mc.lineTo(x, y);
391 }
392
393
394
395
400 public function polygon(points:Array):Void{
401 target_mc.moveTo(points[0].x, points[0].y);
402 for(var i:Number=1; i<points.length; i++){
403 target_mc.lineTo(points[i].x, points[i].y);
404 }
405 target_mc.lineTo(points[0].x, points[0].y);
406 }
407
408
413 public function rectangle(x:Number,y:Number,width:Number,height:Number):Void{
414 target_mc.moveTo(x, y);
415 target_mc.lineTo(x+width,y);
416 target_mc.lineTo(x+width,y+height);
417 target_mc.lineTo(x,y+height);
418 target_mc.lineTo(x,y);
419 }
420
421
426 public function ellipse(x:Number, y:Number, width:Number, height:Number):Void{
427 var pi:Number = Math.PI;
428 var xradius:Number = width/2;
429 var yradius:Number = height/ 2;
430 var cx:Number = x + xradius;
431 var cy:Number = y + yradius;
432 target_mc.moveTo(xradius + cx, 0 + cy);
433 target_mc.curveTo(xradius + cx, (yradius * Math.tan(pi / 8)) + cy, (xradius * Math.cos(pi / 4)) + cx, (yradius * Math.sin(pi / 4)) + cy);
434 target_mc.curveTo((xradius * Math.tan(pi / 8)) + cx, yradius + cy, 0 + cx, yradius + cy);
435 target_mc.curveTo(((-xradius) * Math.tan(pi / 8)) + cx, yradius + cy, ((-xradius) * Math.cos(pi / 4)) + cx, (yradius * Math.sin(pi / 4)) + cy);
436 target_mc.curveTo((-xradius) + cx, (yradius * Math.tan(pi / 8)) + cy, (-xradius) + cx, 0 + cy);
437 target_mc.curveTo((-xradius) + cx, ((-yradius) * Math.tan(pi / 8)) + cy, ((-xradius) * Math.cos(pi / 4)) + cx, ((-yradius) * Math.sin(pi / 4)) + cy);
438 target_mc.curveTo(((-xradius) * Math.tan(pi / 8)) + cx, (-yradius) + cy, 0 + cx, (-yradius) + cy);
439 target_mc.curveTo((xradius * Math.tan(pi / 8)) + cx, (-yradius) + cy, (xradius * Math.cos(pi / 4)) + cx, ((-yradius) * Math.sin(pi / 4)) + cy);
440 target_mc.curveTo(xradius + cx, ((-yradius) * Math.tan(pi / 8)) + cy, xradius + cx, 0 + cy);
441 }
442
443
448 public function circle(cx:Number, cy:Number, r:Number):Void{
449
450 ellipse(cx-r, cy-r, r*2, r*2);
451
452
453
454
455
456 }
457
458
464 public function roundRect(x:Number,y:Number,width:Number,height:Number, radius:Number, trR:Number, blR:Number, brR:Number):Void{
465 var tlR:Number = radius;
466 if(trR == undefined) trR = radius;
467 if(blR == undefined) blR = radius;
468 if(brR == undefined) brR = radius;
469
470 target_mc.moveTo(x+blR, y+height);
471 target_mc.lineTo(x+width-brR, y+height);
472 target_mc.curveTo(x+width, y+height, x+width, y+height-blR);
473
474 target_mc.lineTo (x+width, y+trR);
475 target_mc.curveTo(x+width, y, x+width-trR, y);
476
477 target_mc.lineTo (x+tlR, y);
478 target_mc.curveTo(x, y, x, y+tlR);
479
480 target_mc.lineTo (x, y+height-blR );
481 target_mc.curveTo(x, y+height, x+blR, y+height);
482 }
483
484
487 public function wedge(radius:Number, x:Number, y:Number, angle:Number, rot:Number):Void {
488 target_mc.moveTo(0, 0);
489 target_mc.lineTo(radius, 0);
490 var nSeg:Number = Math.floor(angle/30);
491 var pSeg:Number = angle-nSeg*30;
492 var a:Number = 0.268;
493 var endx:Number;
494 var endy:Number;
495 var ax:Number;
496 var ay:Number;
497 var storeCount:Number=0;
498 for (var i:Number = 0; i<nSeg; i++) {
499 endx = radius*Math.cos((i+1)*30*(Math.PI/180));
500 endy = radius*Math.sin((i+1)*30*(Math.PI/180));
501 ax = endx+radius*a*Math.cos(((i+1)*30-90)*(Math.PI/180));
502 ay = endy+radius*a*Math.sin(((i+1)*30-90)*(Math.PI/180));
503 target_mc.curveTo(ax, ay, endx, endy);
504 storeCount=i+1;
505 }
506 if (pSeg>0) {
507 a = Math.tan(pSeg/2*(Math.PI/180));
508 endx = radius*Math.cos((storeCount*30+pSeg)*(Math.PI/180));
509 endy = radius*Math.sin((storeCount*30+pSeg)*(Math.PI/180));
510 ax = endx+radius*a*Math.cos((storeCount*30+pSeg-90)*(Math.PI/180));
511 ay = endy+radius*a*Math.sin((storeCount*30+pSeg-90)*(Math.PI/180));
512 target_mc.curveTo(ax, ay, endx, endy);
513 }
514 target_mc.lineTo(0, 0);
515 target_mc._rotation = rot;
516 target_mc._x = x;
517 target_mc._y = y;
518 }
519
520
521 }
522