// ***********************************************************************************************
// ______
// .-" "-.
// / AOL // | |
// |, .-. .-. ,|
// | )(__/ \__)( |
// |/ /\ \|
// (@_ (_ ^^ _)
// _ ) \_______\__|IIIIII|__/__________________________
// (_)@8@8{}<________|-\IIIIII/-|___________________________>
// )_/ \ /
// (@ `--------` AOL FLASH STUDIO
//
// ***********************************************************************************************
//
// @FileName Graphic.as
// @Description 图形类
// @Package com.flashvan
// @Author AOL
// @Email jeremy1982@21cn.com
// @Create 2004.7.18
// @LastChange 2004.7.18
// @History
//
// ***********************************************************************************************
dynamic class com.flashvan.Graphic extends MovieClip
{
static var symbolName:String = "__Packages.com.flashvan.Graphic";
static var symbolOwner:Function = com.flashvan.Graphic;
static var symbolLinked = Object.registerClass(symbolName, symbolOwner);
function Graphic()
{
}
static function create(base_mc:MovieClip, name:String, depth:Number,o:Object)
{
var mc = base_mc.attachMovie(symbolName, name, depth,o);
return mc;
}
function drawRect(x1:Number, y1:Number, x2:Number, y2:Number):Void
{
moveTo(x1,y1);
lineTo(x2,y1);
lineTo(x2,y2);
lineTo(x1,y2);
lineTo(x1,y1);
}
function drawLine(x1:Number, y1:Number, x2:Number, y2:Number):Void
{
}
function fillRect(x:Number, y:Number, width:Number, height:Number):Void
{
}
// drawRoundRect
// x - x position of fill
// y - y position of fill
// w - width of fill
// h - height of fill
// r - corner radius of fill :: number or object {br:#,bl:#,tl:#,tr:#}
// c - hex color of fill :: number or array [0x######,0x######]
// alpha - alpha value of fill :: number or array [0x######,0x######]
// rot - rotation of fill :: number or matrix object {matrixType:"box",x:#,y:#,w:#,h:#,r:(#*(Math.PI/180))}
// gradient - type of gradient "linear" or "radial"
// ratios - (optional :: default [0,255]) - specifies the distribution of colors :: array [#,#];
function drawRoundRect(x:Number,y:Number,w:Number,h:Number,r,c,alpha,rot,gradient:String,ratios)
{
if (typeof r == "object") {
var rbr = r.br //bottom right corner
var rbl = r.bl //bottom left corner
var rtl = r.tl //top left corner
var rtr = r.tr //top right corner
}
else
{
var rbr = rbl = rtl = rtr = r;
}
// if color is an object then allow for complex fills
if(typeof c == "object")
{
if (typeof alpha != "object")
var alphas = [alpha,alpha];
else
var alphas = alpha;
if (ratios == undefined)
var ratios = [ 0, 0xff ];
var sh = h *.7
if (typeof rot != "object")
var matrix = {matrixType:"box", x:-sh, y:sh, w:w*2, h:h*4, r:rot * 0.0174532925199433 }
else
var matrix = rot;
if (gradient == "radial")
beginGradientFill( "radial", c, alphas, ratios, matrix );
else
beginGradientFill( "linear", c, alphas, ratios, matrix );
}
else if (c != undefined)
{
beginFill (c, alpha);
}
// Math.sin and Math,tan values for optimal performance.
// Math.rad = Math.PI/180 = 0.0174532925199433
// r*Math.sin(45*Math.rad) = (r*0.707106781186547);
// r*Math.tan(22.5*Math.rad) = (r*0.414213562373095);
//bottom right corner
r = rbr;
var a = r - (r*0.707106781186547); //radius - anchor pt;
var s = r - (r*0.414213562373095); //radius - control pt;
moveTo ( x+w,y+h-r);
lineTo ( x+w,y+h-r );
curveTo( x+w,y+h-s,x+w-a,y+h-a);
curveTo( x+w-s,y+h,x+w-r,y+h);
//bottom left corner
r = rbl;
var a = r - (r*0.707106781186547);
var s = r - (r*0.414213562373095);
lineTo ( x+r,y+h );
curveTo( x+s,y+h,x+a,y+h-a);
curveTo( x,y+h-s,x,y+h-r);
//top left corner
r = rtl;
var a = r - (r*0.707106781186547);
var s = r - (r*0.414213562373095);
lineTo ( x,y+r );
curveTo( x,y+s,x+a,y+a);
curveTo( x+s,y,x+r,y);
//top right
r = rtr;
var a = r - (r*0.707106781186547);
var s = r - (r*0.414213562373095);
lineTo ( x+w-r,y );
curveTo( x+w-s,y,x+w-a,y+a);
curveTo( x+w,y+s,x+w,y+r);
lineTo ( x+w,y+h-r );
if (c != undefined)
endFill();
}
// ==============
// mc.drawOval() - by Ric Ewing (ric@formequalsfunction.com) - version 1.1 - 4.7.2002
//
// x, y = center of oval
// radius = radius of oval. If [optional] yRadius is defined, r is the x radius.
// yRadius = [optional] y radius of oval.
// ==============
function drawOval(x:Number, y:Number, radius:Number, yRadius:Number):Void
{
if (arguments.length<3)
{
return;
}
// init variables
var theta:Number, xrCtrl:Number, yrCtrl:Number, angle:Number, angleMid:Number;
var px:Number, py:Number, cx:Number, cy:Number;
// if only yRadius is undefined, yRadius = radius
if (yRadius == undefined)
{
yRadius = radius;
}
// covert 45 degrees to radians for our calculations
theta = Math.PI/4;
// calculate the distance for the control point
xrCtrl = radius/Math.cos(theta/2);
yrCtrl = yRadius/Math.cos(theta/2);
// start on the right side of the circle
angle = 0;
moveTo(x+radius, y);
// this loop draws the circle in 8 segments
for (var i = 0; i<8; i++)
{
// increment our angles
angle += theta;
angleMid = angle-(theta/2);
// calculate our control point
cx = x+Math.cos(angleMid)*xrCtrl;
cy = y+Math.sin(angleMid)*yrCtrl;
// calculate our end point
px = x+Math.cos(angle)*radius;
py = y+Math.sin(angle)*yRadius;
// draw the circle segment
curveTo(cx, cy, px, py);
}
}
// ==============
// mc.drawArc() - by Ric Ewing (ric@formequalsfunction.com) - version 1.5 - 4.7.2002
//
// x, y = This must be the current pen position... other values will look bad
// radius = radius of Arc. If [optional] yRadius is defined, then r is the x radius
// arc = sweep of the arc. Negative values draw clockwise.
// startAngle = starting angle in degrees.
// yRadius = [optional] y radius of arc. Thanks to Robert Penner for the idea.
// ==============
// Thanks to: Robert Penner, Eric Mueller and Michael Hurwicz for their contributions.
// ==============
function drawArc(x:Number, y:Number, radius:Number, arc:Number, startAngle:Number, yRadius:Number)
{
if (arguments.length<5)
{
return;
}
// if yRadius is undefined, yRadius = radius
if (yRadius == undefined)
{
yRadius = radius;
}
// Init vars
var segAngle:Number, theta:Number, angle:Number, angleMid:Number, segs:Number;
var ax:Number, ay:Number, bx:Number, by:Number, cx:Number, cy:Number;
// no sense in drawing more than is needed :)
if (Math.abs(arc)>360)
{
arc = 360;
}
// Flash uses 8 segments per circle, to match that, we draw in a maximum
// of 45 degree segments. First we calculate how many segments are needed
// for our arc.
segs = Math.ceil(Math.abs(arc)/45);
// Now calculate the sweep of each segment
segAngle = arc/segs;
// The math requires radians rather than degrees. To convert from degrees
// use the formula (degrees/180)*Math.PI to get radians.
theta = -(segAngle/180)*Math.PI;
// convert angle startAngle to radians
angle = -(startAngle/180)*Math.PI;
// find our starting points (ax,ay) relative to the secified x,y
ax = x-Math.cos(angle)*radius;
ay = y-Math.sin(angle)*yRadius;
// if our arc is larger than 45 degrees, draw as 45 degree segments
// so that we match Flash's native circle routines.
if (segs>0)
{
// Loop for drawing arc segments
for (var i = 0; i// increment our angle
angle += theta;
// find the angle halfway between the last angle and the new
angleMid = angle-(theta/2);
// calculate our end point
bx = ax+Math.cos(angle)*radius;
by = ay+Math.sin(angle)*yRadius;
// calculate our control point
cx = ax+Math.cos(angleMid)*(radius/Math.cos(theta/2));
cy = ay+Math.sin(angleMid)*(yRadius/Math.cos(theta/2));
// draw the arc segment
curveTo(cx, cy, bx, by);
}
}
// In the native draw methods the user must specify the end point
// which means that they always know where they are ending at, but
// here the endpoint is unknown unless the user calculates it on their
// own. Lets be nice and let save them the hassle by passing it back.
return {x:bx, y:by};
}
// ==============
// mc.drawBurst() - by Ric Ewing (ric@formequalsfunction.com) - version 1.4 - 4.7.2002
//
// x, y = center of burst
// sides = number of sides or points
// innerRadius = radius of the indent of the curves
// outerRadius = radius of the outermost points
// angle = [optional] starting angle in degrees. (defaults to 0)
// ==============
function drawBurst(x:Number, y:Number, sides:Number, innerRadius:Number, outerRadius:Number, angle:Number)
{
if(arguments<5)
{
return;
}
if (sides>2)
{
// init vars
var step:Number, halfStep:Number, qtrStep:Number, start:Number, n:Number;
var dx:Number, dy:Number, cx:Number, cy:Number;
// calculate length of sides
step = (Math.PI*2)/sides;
halfStep = step/2;
qtrStep = step/4;
// calculate starting angle in radians
start = (angle/180)*Math.PI;
moveTo(x+(Math.cos(start)*outerRadius), y-(Math.sin(start)*outerRadius));
// draw curves
for (n=1; n<=sides; n++)
{
cx = x+Math.cos(start+(step*n)-(qtrStep*3))*(innerRadius/Math.cos(qtrStep));
cy = y-Math.sin(start+(step*n)-(qtrStep*3))*(innerRadius/Math.cos(qtrStep));
dx = x+Math.cos(start+(step*n)-halfStep)*innerRadius;
dy = y-Math.sin(start+(step*n)-halfStep)*innerRadius;
curveTo(cx, cy, dx, dy);
cx = x+Math.cos(start+(step*n)-qtrStep)*(innerRadius/Math.cos(qtrStep));
cy = y-Math.sin(start+(step*n)-qtrStep)*(innerRadius/Math.cos(qtrStep));
dx = x+Math.cos(start+(step*n))*outerRadius;
dy = y-Math.sin(start+(step*n))*outerRadius;
curveTo(cx, cy, dx, dy);
}
}
};
function drawPoly (x:Number, y:Number, sides:Number, radius:Number, angle:Number)
{
if (arguments.length<4)
{
return;
}
// convert sides to positive value
var count:Number = Math.abs(sides);
// check that count is sufficient to build polygon
if (count>2)
{
// init vars
var step:Number, start:Number, n:Number, dx:Number, dy:Number;
// calculate span of sides
step = (Math.PI*2)/sides;
// calculate starting angle in radians
start = (angle/180)*Math.PI;
moveTo(x+(Math.cos(start)*radius), y-(Math.sin(start)*radius));
// draw the polygon
for (n=1; n<=count; n++)
{
dx = x+Math.cos(start+(step*n))*radius;
dy = y-Math.sin(start+(step*n))*radius;
lineTo(dx, dy);
}
}
}
// ==============
// mc.drawWedge() - by Ric Ewing (ric@formequalsfunction.com) - version 1.3 - 6.12.2002
//
// x, y = center point of the wedge.
// startAngle = starting angle in degrees.
// arc = sweep of the wedge. Negative values draw clockwise.
// radius = radius of wedge. If [optional] yRadius is defined, then radius is the x radius.
// yRadius = [optional] y radius for wedge.
// ==============
// Thanks to: Robert Penner, Eric Mueller and Michael Hurwicz for their contributions.
// ==============
function drawWedge(x:Number, y:Number, startAngle:Number, arc:Number, radius:Number, yRadius:Number)
{
if (arguments.length<5)
{
return;
}
// move to x,y position
moveTo(x, y);
// if yRadius is undefined, yRadius = radius
if (yRadius == undefined)
{
yRadius = radius;
}
// Init vars
var segAngle:Number, theta:Number, angle:Number, angleMid:Number, segs:Number;
var ax:Number, ay:Number, bx:Number, by:Number, cx:Number, cy:Number;
// limit sweep to reasonable numbers
if (Math.abs(arc)>360)
{
arc = 360;
}
// Flash uses 8 segments per circle, to match that, we draw in a maximum
// of 45 degree segments. First we calculate how many segments are needed
// for our arc.
segs = Math.ceil(Math.abs(arc)/45);
// Now calculate the sweep of each segment.
segAngle = arc/segs;
// The math requires radians rather than degrees. To convert from degrees
// use the formula (degrees/180)*Math.PI to get radians.
theta = -(segAngle/180)*Math.PI;
// convert angle startAngle to radians
angle = -(startAngle/180)*Math.PI;
// draw the curve in segments no larger than 45 degrees.
if (segs>0)
{
// draw a line from the center to the start of the curve
ax = x+Math.cos(startAngle/180*Math.PI)*radius;
ay = y+Math.sin(-startAngle/180*Math.PI)*yRadius;
lineTo(ax, ay);
// Loop for drawing curve segments
for (var i = 0; iMath.cos(angle)*radius;
by = y+Math.sin(angle)*yRadius;
cx = x+Math.cos(angleMid)*(radius/Math.cos(theta/2));
cy = y+Math.sin(angleMid)*(yRadius/Math.cos(theta/2));
curveTo(cx, cy, bx, by);
}
// close the wedge by drawing a line to the center
lineTo(x, y);
}
}
}
//end of Class
在riacn看到一个转的国外人写的Graphic类,感觉不是很好。 首选它是从MovieClip类继承下来的,但它却
又用_root这个根时间轴来创建一个MovieClip的实例mc来mix-in到Graphic中。这样做是很不对的!并且一系列的
MovieClip的方法要通过mc来重写实现!我几个月前写的这个类虽然不是很充分,因为这是我组件库所有View的基类,
很多方法在考虑中,但绝对继承了MovieClip的所有方法和属性,而且跟MovieClip类一样是dynamic的.比起国外那
个,这个更有科学性.