这是年前的代码,全部共享给大家。其中主GDI类中的绘制及填充方法还不全,如多边形、圆角矩形等,以后再补上吧。具体结构可参考:我的Flash《JGDI图形绘制类》开发心得及结构图
以下为类代码:
/*临时temp @class temp.graphics.JGDI @package temp.graphics @author 高翔 @tooltip JGDI类,目前有绘制矩形、点、直线、圆或椭圆方法和填充矩形方法 @Create 2004.12.5 @LastChange 2004.12.20 */ import temp.graphics.JPen; import temp.graphics.JBrush; import temp.graphics.JSolidBrush; import temp.graphics.JGradientBrush; //JGDI类 class temp.graphics.JGDI { //私有的_target属性 private static var _instance:JGDI; private var _target:MovieClip; //单例模式,只允许一个绘图实例 public static function getInstance():JGDI { if (JGDI._instance == undefined) { JGDI._instance = new JGDI(); } return JGDI._instance; } //私用构造 private function JGDI() { } public function get target():MovieClip { return _target; } public function set target(target:MovieClip):Void { this._target = target; trace(_target); } //开始笔 private function startPen(pen:JPen) { trace(pen.width); _target.lineStyle(pen.width, pen.color, pen.alpha); } //结束笔 private function endPen() { _target.lineStyle(null, null, null); } //开始笔刷 private function startBrush(b:JBrush) { if (b instanceof JSolidBrush) { b = JSolidBrush(b); } else if (b instanceof JGradientBrush) { b = JGradientBrush(b); } b.fill(_target); } //结束笔刷 private function endBrush():Void { _target.endFill(); } //绘制矩形方法(画笔、初点的x、y坐标、长、宽) public function drawRect(pen:JPen, x:Number, y:Number, h:Number, w:Number):Void { startPen(pen); //_target.lineStyle(pen.width, pen.color, pen.alpha); _target.moveTo(x, y); _target.lineTo(x+w, y); _target.lineTo(x+w, y+h); _target.lineTo(x, y+h); _target.lineTo(x, y); //endBrush(); endPen(); } //填充矩形方法(笔刷、初点的x、y坐标、长、宽) public function fillRect(brush:JBrush, x:Number, y:Number, h:Number, w:Number):Void { startBrush(brush); _target.moveTo(x, y); _target.lineTo(x+w, y); _target.lineTo(x+w, y+h); _target.lineTo(x, y+h); _target.lineTo(x, y); endBrush(); } // 绘制点(初点的x、y坐标及s半径) public function drawPoint(pen:JPen, x:Number, y:Number, s:Number):Void { _target.lineStyle(pen.width, pen.color, pen.alpha); _target.moveTo(x, y); _target.lineTo(x+s, y); _target.lineTo(x+s, y+s); _target.lineTo(x, y+s); _target.lineTo(x, y); } //绘制直线(初点的x1、y1坐标,终点的x2、y2坐标) public function drawLine(pen:JPen, x1:Number, y1:Number, x2:Number, y2:Number) { startPen(pen); _target.moveTo(x1, y1); _target.lineTo(x2, y2); endPen(); } //绘制圆或椭圆方法(画笔、圆心的x、y坐标、x轴半径、y轴半径) public function drawCircle(p:JPen, x:Number, y:Number, rx:Number, ry:Number) { startPen(p); _target.moveTo(x+rx, y); _target.curveTo(rx+x, 0.4142*ry+y, 0.7071*rx+x, 0.7071*ry+y); _target.curveTo(0.4142*rx+x, ry+y, x, ry+y); _target.curveTo(-0.4142*rx+x, ry+y, -0.7071*rx+x, 0.7071*ry+y); _target.curveTo(-rx+x, 0.4142*ry+y, -rx+x, y); _target.curveTo(-rx+x, -0.4142*ry+y, -0.7071*rx+x, -0.7071*ry+y); _target.curveTo(-0.4142*rx+x, -ry+y, x, -ry+y); _target.curveTo(0.4142*rx+x, -ry+y, 0.7071*rx+x, -0.7071*ry+y); _target.curveTo(rx+x, -0.4142*ry+y, rx+x, y); _target.endPen(); } } |
/*临时temp @class temp.graphics.JColor @package temp.graphics @author 高翔 @tooltip 静态的JColor类 @Create 2004.12.12 @LastChange 2004.12.12 */ class temp.graphics.JColor { //white白色 public static var white:Number = 0xffffff; //lightGray亮灰 public static var lightGray:Number = 0xc0c0c0; //gray灰 public static var gray:Number = 0x808080; //darkGray深灰 public static var darkGray:Number = 0x404040; //black黑 public static var black:Number = 0x000000; //red红 public static var red:Number = 0xff0000; //pink粉红 public static var pink:Number = 0xffafaf; //orange橙 public static var orange:Number = 0xffc800; //yellow黄 public static var yellow:Number = 0xffff00; //green绿 public static var green:Number = 0x00ff00; //magenta洋红/紫 public static var magenta:Number = 0xff00ff; //cyan青 public static var cyan:Number = 0x00ffff; //blue蓝 public static var blue:Number = 0x0000ff; private function JColor() { } //静态方法,返回颜色值。如JColor.rgb(255, 0, 0)则为绿色,返回数值65280 public static function rgb(r:Number, g:Number, b:Number):Number { var color_n:Number = Number("0x"+r.toString(16)+g.toString(16)+b.toString(16)); return color_n; } } |
/*临时temp @class temp.graphics.JPen @package temp.graphics @author 高翔 @tooltip 测试笔刷JPen类 @Create 2004.12.5 @LastChange 2004.12.5 */ class temp.graphics.JPen { private var _width:Number; private var _color:Number; private var _alpha:Number; //构造器(宽度、颜色、透明度) public function JPen(width:Number, color:Number, alpha:Number) { _width = width; _color = color; _alpha = alpha; } //获取属性 public function get width():Number { return _width; } public function get color():Number { return _color; } public function get alpha():Number { return _alpha; } //设置属性 public function set width(width:Number):Void { this._width = width; } public function set color(color:Number):Void { this._color = color; } public function set alpha(alpha:Number):Void { this._alpha = alpha; } } |
/*临时temp @class temp.graphics.JSolidBrush @package temp.graphics @author 高翔 @tooltip 测试笔刷JSolidBrush类 @Create 2004.12.17 @LastChange 2004.12.17 */ class temp.graphics.JBrush { //抽象笔刷类型 private var _type; private function JBrush() { } public function getType():String { return _type; } public function fill(target:MovieClip) { } } |
/*临时temp @class temp.graphics.JSolidBrush @package temp.graphics @author 高翔 @tooltip 测试笔刷JSolidBrush类 @Create 2004.12.13 @LastChange 2004.12.17 */ import temp.graphics.JBrush; class temp.graphics.JSolidBrush extends JBrush { private var _color:Number; private var _alpha:Number; //实例化笔刷,具有color和alpha两个属性 public function JSolidBrush(color:Number, alpha:Number) { _color = color; _alpha = alpha; _type = "JSolidBrush"; } //扩展笔刷类方法 public function fill(target:MovieClip) { target.beginFill(_color, _alpha); } } |
/*临时temp @class temp.graphics.JGradientBrush @package temp.graphics @author 高翔 @tooltip 测试笔刷JGradientBrush类 @Create 2004.12.20 @LastChange 2004.12.20 */ import temp.graphics.JBrush; class temp.graphics.JGradientBrush extends JBrush { private var _fillType:String; private var _colors:Array; private var _alphas:Array; private var _ratios:Array; private var _matrix:Object; public function JGradientBrush(fillType:String, colors:Array, alphas:Array, ratios:Array, matrix:Object) { this._fillType = fillType; this._colors = colors; this._alphas = alphas; this._ratios = ratios; this._matrix = matrix; _type = "JGradientBrush"; } //扩展笔刷类方法 public function fill(target:MovieClip) { target.beginGradientFill(_fillType, _colors, _alphas, _ratios, _matrix); } } |
在Flash中使用方法:
//----------测试代码 高翔-2004.12.20---------- //插入测试绘图类 import temp.graphics.*; //单例模式 test = JGDI.getInstance(); //设置绘制目标 test.target = _root; //绘制矩形一 test.drawRect(new JPen(1, JColor.blue, 100), 0, 0, 50, 50); //使用JGradientBrush过渡笔刷填充矩形 var colors = [0xffff00, 0xff0000]; var alphas = [100, 100]; var ratios = [0, 0xFF]; var matrix = {a:50, b:0, c:0, d:0, e:50, f:0, g:25, h:50, i:1}; test.fillRect(new JGradientBrush("linear", colors, alphas, ratios, matrix), 0, 0, 50, 50); //test.fillRect(new JGradientBrush("radial", colors, alphas, ratios, matrix), 0, 0, 50, 50); //绘制矩形二 test.drawRect(new JPen(1, JColor.green, 100), 75, 0, 50, 50); //使用JSolidBrush单色笔刷填充矩形@使用JColor的黄色,alphas为10 //以下颜色属性可参考JColor类 test.fillRect(new JSolidBrush(JColor.yellow, 10), 75, 0, 50, 50); //绘制圆 test.drawCircle(new JPen(1, JColor.red, 100), 75, 25, 25, 25); //绘制椭圆 test.drawCircle(new JPen(1, JColor.red, 100), 150, 25, 50, 20); //绘制线 test.drawLine(new JPen(1, JColor.magenta, 100), 0, 60, 100, 60); //绘制点 test.drawPoint(new JPen(1, JColor.black, 100), 110, 60, 2); |
好了,至此我们运行一下代码就可以看到上面图片的效果了。^-^
大家有什么问题或意见尽管指出。