设为首页收藏本站

新微赢技术网

 找回密码
 注册
搜索
热搜: 回贴
查看: 7626|回复: 1
打印 上一主题 下一主题

AS2.0的Graphics类

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-28 00:53:20 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
功能说明:
设置图形的颜色,粗细和透明度;
画直线 drawLine;
画曲线 drawCurve;
画矩形 drawRect;
填充矩形 fillRect;
画圆形 drawCircle;
填充圆形 fillCircle;
画椭圆 drawOval;
填充椭圆 fillOval;


程序代码
以下是Graphics.as类代码段:
/*
* Name:Graphics.as
* Author:Kinglong
* Email:qiankinglong@hotmail.com
* Date:2004-11-4
* Ver:0.60
*/
class com.klstudio.Graphics extends MovieClip {
private var __clr:Number;
private var __mc:MovieClip;
private var __thick:Number;
function Graphics(_mc:MovieClip) {
  if (_mc == undefined || _mc == null) {
   this.__mc = _root;
  } else {
   this.__mc = _mc;
  }
  this.setColor(0x000000);
  this.setThick(1);
}
//设置线框色;
public function setColor(_color:Number):Void {
  this.__clr = _color;
}
//设置线框宽度;
public function setThick(_thick:Number):Void {
  this.__thick = _thick;
}
//画直线;
public function drawLine(_x1:Number, _y1:Number, _x2:Number, _y2:Number, _a:Number):Void {
  if (arguments.length<4) {
   trace("※错误※"+newline+"参数不足!");
  } else {
   if (_a == undefined || _a == null) {
    _a = 100;
   }
   this.__mc.lineStyle(this.__thick, this.__clr, _a);
   this.__mc.moveTo(_x1, _y1);
   this.__mc.lineTo(_x2, _y2);
  }
}
//画矩形;
public function drawRect(_x0:Number, _y0:Number, _w:Number, _h:Number, _a:Number):Void {
  if (arguments.length<4) {
   trace("※错误※"+newline+"参数不足!");
  } else {
   if (_a == undefined || _a == null) {
    _a = 100;
   }
   this.__mc.lineStyle(this.__thick, this.__clr, _a);
   this.__mc.moveTo(_x0, _y0);
   this.__mc.lineTo(_x0+_w, _y0);
   this.__mc.lineTo(_x0+_w, _y0+_h);
   this.__mc.lineTo(_x0, _y0+_h);
   this.__mc.lineTo(_x0, _y0);
  }
}
//填充矩形;
public function fillRect(_x0:Number, _y0:Number, _w:Number, _h:Number, _a:Number):Void {
  if (arguments.length<4) {
   trace("※错误※"+newline+"参数不足!");
  } else {
   if (_a == undefined || _a == null) {
    _a = 100;
   }
   this.__mc.lineStyle();
   this.__mc.moveTo(_x0, _y0);
   this.__mc.beginFill(this.__clr, _a);
   this.__mc.lineTo(_x0+_w, _y0);
   this.__mc.lineTo(_x0+_w, _y0+_h);
   this.__mc.lineTo(_x0, _y0+_h);
   this.__mc.lineTo(_x0, _y0);
   this.__mc.endFill();
  }
}
//画曲线;
public function drawCurve(_sx:Number, _sy:Number, _cx:Number, _cy:Number, _ex:Number, _ey:Number, _a:Number):Void {
  if (arguments.length<6) {
   trace("※错误※"+newline+"参数不足!");
  } else {
   if (_a == undefined || _a == null) {
    _a = 100;
   }
   this.__mc.lineStyle(this.__thick, this.__clr, _a);
   this.__mc.moveTo(_sx, _sy);
   this.__mc.curveTo(_cx, _cy, _ex, _ey);
  }
}
//画椭圆;
function drawOval(_x0:Number, _y0:Number, _w:Number, _h:Number, _a:Number):Void {
  if (arguments.length<4) {
   trace("※错误※"+newline+"参数不足!");
  } else {
   if (_a == undefined || _a == null) {
    _a = 100;
   }
   var j:Number = _w*0.70711;
   var n:Number = _h*0.70711;
   var i:Number = j-(_h-n)*_w/_h;
   var m:Number = n-(_w-j)*_h/_w;
   this.__mc.lineStyle(this.__thick, this.__clr, _a);
   this.__mc.moveTo(_x0+_w, _y0);
   this.__mc.curveTo(_x0+_w, _y0-m, _x0+j, _y0-n);
   this.__mc.curveTo(_x0+i, _y0-_h, _x0, _y0-_h);
   this.__mc.curveTo(_x0-i, _y0-_h, _x0-j, _y0-n);
   this.__mc.curveTo(_x0-_w, _y0-m, _x0-_w, _y0);
   this.__mc.curveTo(_x0-_w, _y0+m, _x0-j, _y0+n);
   this.__mc.curveTo(_x0-i, _y0+_h, _x0, _y0+_h);
   this.__mc.curveTo(_x0+i, _y0+_h, _x0+j, _y0+n);
   this.__mc.curveTo(_x0+_w, _y0+m, _x0+_w, _y0);
  }
}
//填充椭圆;
function fillOval(_x0:Number, _y0:Number, _w:Number, _h:Number, _a:Number):Void {
  if (arguments.length<4) {
   trace("※错误※"+newline+"参数不足!");
  } else {
   if (_a == undefined || _a == null) {
    _a = 100;
   }
   var j:Number = _w*0.70711;
   var n:Number = _h*0.70711;
   var i:Number = j-(_h-n)*_w/_h;
   var m:Number = n-(_w-j)*_h/_w;
   this.__mc.lineStyle();
   this.__mc.moveTo(_x0+_w, _y0);
   this.__mc.beginFill(this.__clr, _a);
   this.__mc.curveTo(_x0+_w, _y0-m, _x0+j, _y0-n);
   this.__mc.curveTo(_x0+i, _y0-_h, _x0, _y0-_h);
   this.__mc.curveTo(_x0-i, _y0-_h, _x0-j, _y0-n);
   this.__mc.curveTo(_x0-_w, _y0-m, _x0-_w, _y0);
   this.__mc.curveTo(_x0-_w, _y0+m, _x0-j, _y0+n);
   this.__mc.curveTo(_x0-i, _y0+_h, _x0, _y0+_h);
   this.__mc.curveTo(_x0+i, _y0+_h, _x0+j, _y0+n);
   this.__mc.curveTo(_x0+_w, _y0+m, _x0+_w, _y0);
   this.__mc.endFill();
  }
}
//画圆形;
function drawCircle(_r:Number, _x0:Number, _y0:Number, _a:Number):Void {
  if (arguments.length<3) {
   trace("※错误※"+newline+"参数不足!");
  } else {
   if (_a == undefined || _a == null) {
    _a = 100;
   }
   this.__mc.lineStyle(this.__thick, this.__clr, _a);
   this.__mc.moveTo(_x0+_r, _y0);
   var _s:Number = Math.tan(22.5*Math.PI/180);
   for (var angle = 45; angle<=360; angle += 45) {
    var endX:Number = _r*Math.cos(angle*Math.PI/180);
    var endY:Number = _r*Math.sin(angle*Math.PI/180);
    var cX:Number = endX+_r*_s*Math.cos((angle-90)*Math.PI/180);
    var cY:Number = endY+_r*_s*Math.sin((angle-90)*Math.PI/180);
    this.__mc.curveTo(cX+_x0, cY+_y0, endX+_x0, endY+_y0);
   }
  }
}
//填充圆形;
function fillCircle(_r:Number, _x0:Number, _y0:Number, _a:Number):Void {
  if (arguments.length<3) {
   trace("※错误※"+newline+"参数不足!");
  } else {
   if (_a == undefined || _a == null) {
    _a = 100;
   }
   this.__mc.lineStyle();
   this.__mc.moveTo(_x0+_r, _y0);
   var _s:Number = Math.tan(22.5*Math.PI/180);
   this.__mc.beginFill(this.__clr, _a);
   for (var angle = 45; angle<=360; angle += 45) {
    var endX:Number = _r*Math.cos(angle*Math.PI/180);
    var endY:Number = _r*Math.sin(angle*Math.PI/180);
    var cX:Number = endX+_r*_s*Math.cos((angle-90)*Math.PI/180);
    var cY:Number = endY+_r*_s*Math.sin((angle-90)*Math.PI/180);
    this.__mc.curveTo(cX+_x0, cY+_y0, endX+_x0, endY+_y0);
   }
   this.__mc.endFill();
  }
}
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

申请友链|小黑屋|最新主题|手机版|新微赢技术网 ( 苏ICP备08020429号 )  

GMT+8, 2024-11-18 19:50 , Processed in 0.183988 second(s), 9 queries , Gzip On, Memcache On.

Powered by xuexi

© 2001-2013 HaiAn.Com.Cn Inc. 寰耽

快速回复 返回顶部 返回列表