//绘图类
import fc.graphics*;
import fc.graphics.draw2d.AbstractDraw;
class fc.graphics.draw2d.GDI {
private var _target:MovieClip;
//设置和获取绘图的目标
public function get target() {
return _target;
}
public function set target(mc) {
_target = mc;
}
//画线方法
public function line(p:Pen, d:AbstractDraw) {
startPen(p);
d.target = _target;
d.draw();
endPen();
}
//填充方法
public function fill(b:Brush, d:AbstractDraw) {
startBrush(b);
d.target = _target;
d.draw();
endBrush();
}
private function startPen(p) {
_target.lineStyle(p.width, p.color, p.alpha);
}
private function endPen():Void {
_target.lineStyle(null, null, null);
}
private function startBrush(b:Brush) {
b.fill(_target);
}
private function endBrush() {
_target.endFill();
}
//清除绘图方法
public function clear():Void {
if (_target != null) {
_target.clear();
}
}
}
//抽象画图类
class fc.graphics.draw2d.AbstractDraw {
private var _target:MovieClip;
public function get target() {
return _target;
}
public function set target(mc) {
_target = mc;
}
//定义统一接口
function draw(){
}
}
//画直线
import fc.graphics.draw2d.AbstractDraw;
class fc.graphics.draw2d.Line extends AbstractDraw {
private var _x1:Number;
private var _x2:Number;
private var _y1:Number;
private var _y2:Number;
public function Line(x1:Number, y1:Number, x2:Number, y2:Number) {
_x1 = x1;
_x2 = x2;
_y1 = y1;
_y2 = y2;
}
//绘制实直线
public function draw(){
lineFunc(_x1, _y1, _x2, _y2);
}
//绘制直线函数
private function lineFunc(x1:Number, y1:Number, x2:Number, y2:Number) {
_target.moveTo(x1, y1);
_target.lineTo(x2, y2);
}
}
//画矩形
import fc.graphics.draw2d.AbstractDraw;
class fc.graphics.draw2d.Rect extends AbstractDraw {
private var _x:Number;
private var _y:Number;
private var _w:Number;
private var _h:Number;
public function Rect(x:Number, y:Number, w:Number, h:Number) {
_x = x;
_y = y;
_w = w;
_h = h;
}
public function draw() {
rectFunc(_x, _y, _w, _h);
}
//绘制矩形
private function rectFunc(x:Number, y:Number, w:Number, h:Number) {
_target.moveTo(x, y);
_target.lineTo(x+w, y);
_target.lineTo(x+w, y+h);
_target.lineTo(x, y+h);
_target.lineTo(x, y);
}
}
//画正多边
import fc.graphics.draw2d.AbstractDraw
class fc.graphics.draw2d.Poly extends AbstractDraw {
private var _x:Number;
private var _y:Number;
private var _r:Number;
private var _n:Number;
public function Poly(x:Number, y:Number, r:Number, n:Number) {
_x = x;
_y = y;
_r = r;
_n = n;
}
public function draw() {
polyFunc(_x, _y, _r, _n);
}
//绘制多边形
private function polyFunc(x:Number, y:Number, r:Number, n:Number) {
var pai = 2*Math.PI;
_target.moveTo(r+x, y);
for (var i = 0; i<pai/n*(n+1); i += pai/n) {
_target.lineTo(Math.cos(i)*r+x, Math.sin(i)*r+y);
}
}
}
//椭圆,圆
import fc.graphics.draw2d.AbstractDraw;
import fc.math.Degree;
class fc.graphics.draw2d.Ellipse extends AbstractDraw {
private var _x:Number;
private var _y:Number;
private var _rx:Number;
private var _ry:Number;
public function Ellipse(x:Number, y:Number, rx:Number, ry:Number) {
_x = x;
_y = y;
_rx = rx;
_ry = ry;
}
public function draw() {
ellipseFunc(_x, _y, _rx, _ry);
}
//绘制椭圆(圆)函数
public function ellipseFunc(x:Number, y:Number, rx:Number, ry:Number) {
if (ry == null) {
ry = rx;
}
var controlx = rx*(Math.SQRT2-1);
var controly = ry*(Math.SQRT2-1);
var anchorx = rx*Math.SQRT1_2;
var anchory = ry*Math.SQRT1_2;
_target.moveTo(x+rx, y);
_target.curveTo(x+rx, y+controly, x+anchorx, y+anchory);
_target.curveTo(x+controlx, y+ry, x, y+ry);
_target.curveTo(x-controlx, y+ry, x-anchorx, y+anchory);
_target.curveTo(x-rx, y+controly, x-rx, y);
_target.curveTo(x-rx, y-controly, x-anchorx, y-anchory);
_target.curveTo(x-controlx, y-ry, x, y-ry);
_target.curveTo(x+controlx, y-ry, x+anchorx, y-anchory);
_target.curveTo(x+rx, y-controly, x+rx, y);
}
}
//弧线
import fc.math.Degree;
import fc.graphics.draw2d.Ellipse;
//绘制椭圆或圆形曲线
class fc.graphics.draw2d.Arc extends Ellipse {
private var _staAngle;
private var _endAngle;
public function Arc(x:Number, y:Number, rx:Number, ry:Number, staAngle,
endAngle) {
_x = x;
_y = y;
_rx = rx;
_ry = ry;
_staAngle = staAngle;
_endAngle = endAngle;
}
public function draw() {
arcFunc(_x, _y, _rx, _ry, _staAngle, _endAngle);
}
//绘制椭圆(圆)形曲线
public function arcFunc(x, y, rx, ry, staAngle, endAngle) {
if (endAngle == staAngle) {
return;
}
if (Math.abs(endAngle-staAngle)>=360) {
ellipseFunc(x, y, rx, ry);
return;
}
if (ry == null) {
ry = rx;
}
var curAngle = staAngle;
var nextAngle = staAngle<endAngle ? (Math.floor(staAngle/45)+1)*45 :
(Math.ceil(staAngle/45)-1)*45;
var midcos, midAngle, controlx, controly, anchorx, anchory;
_target.moveTo(x+Degree.cosD(staAngle)*rx,
y+Degree.sinD(staAngle)*ry);
while (true) {
if (staAngle<endAngle && nextAngle>endAngle || staAngle>endAngle
&& nextAngle<endAngle) {
nextAngle = endAngle;
}
midcos = Degree.cosD((nextAngle-curAngle)/2);
midAngle = (nextAngle+curAngle)/2;
controlx = x+Degree.cosD(midAngle)/midcos*rx;
controly = y+Degree.sinD(midAngle)/midcos*ry;
anchorx = x+Degree.cosD(nextAngle)*rx;
anchory = y+Degree.sinD(nextAngle)*ry;
_target.curveTo(controlx, controly, anchorx, anchory);
if (nextAngle == endAngle) {
break;
}
curAngle = nextAngle;
nextAngle += staAngle<endAngle ? 45 : -45;
}
}
}
//扇形
import fc.math.Degree;
import fc.graphics.draw2d.Arc;
class fc.graphics.draw2d.Sector extends Arc {
public function Sector(x:Number, y:Number, rx:Number, ry:Number, staAngle,
endAngle) {
_x = x;
_y = y;
_rx = rx;
_ry = ry;
_staAngle = staAngle;
_endAngle = endAngle;
}
public function draw() {
sectorFunc(_x, _y, _rx, _ry, _staAngle, _endAngle);
}
//绘制扇形
private function sectorFunc(x, y, rx, ry, staAngle, endAngle) {
arcFunc(x, y, rx, ry, staAngle, endAngle);
if (Math.abs(staAngle-endAngle)>0 && Math.abs(staAngle-endAngle)<360)
{
_target.lineTo(x, y);
_target.lineTo(x+Degree.cosD(staAngle)*rx,
y+Degree.sinD(staAngle)*ry);
}
}
}
画图测试
//导入所需的类
import fc.graphics.*;
import fc.graphics.draw2d.*;
//创建绘图实例
var test:GDI = new GDI();
//定义绘图目标
test.target = this;
//定义钢笔样式
var pen = new Pen(5, Color.green, 100);
//定义填充样式
var brush = new SolidBrush(Color.green, 100);
//绘制填充椭圆
test.fill(brush, new Rect(100, 100, 100, 100));
//绘制直线
test.line(pen, new Line(0, 0, 100, 100));
//绘制多边形
test.line(pen, new Poly(200, 150, 200, 5));
//用一个按钮清除绘图
clearBtn.onRelease = function() {
test.clear();
};
import fc.graphics*;
import fc.graphics.draw2d.AbstractDraw;
class fc.graphics.draw2d.GDI {
private var _target:MovieClip;
//设置和获取绘图的目标
public function get target() {
return _target;
}
public function set target(mc) {
_target = mc;
}
//画线方法
public function line(p:Pen, d:AbstractDraw) {
startPen(p);
d.target = _target;
d.draw();
endPen();
}
//填充方法
public function fill(b:Brush, d:AbstractDraw) {
startBrush(b);
d.target = _target;
d.draw();
endBrush();
}
private function startPen(p) {
_target.lineStyle(p.width, p.color, p.alpha);
}
private function endPen():Void {
_target.lineStyle(null, null, null);
}
private function startBrush(b:Brush) {
b.fill(_target);
}
private function endBrush() {
_target.endFill();
}
//清除绘图方法
public function clear():Void {
if (_target != null) {
_target.clear();
}
}
}
//抽象画图类
class fc.graphics.draw2d.AbstractDraw {
private var _target:MovieClip;
public function get target() {
return _target;
}
public function set target(mc) {
_target = mc;
}
//定义统一接口
function draw(){
}
}
//画直线
import fc.graphics.draw2d.AbstractDraw;
class fc.graphics.draw2d.Line extends AbstractDraw {
private var _x1:Number;
private var _x2:Number;
private var _y1:Number;
private var _y2:Number;
public function Line(x1:Number, y1:Number, x2:Number, y2:Number) {
_x1 = x1;
_x2 = x2;
_y1 = y1;
_y2 = y2;
}
//绘制实直线
public function draw(){
lineFunc(_x1, _y1, _x2, _y2);
}
//绘制直线函数
private function lineFunc(x1:Number, y1:Number, x2:Number, y2:Number) {
_target.moveTo(x1, y1);
_target.lineTo(x2, y2);
}
}
//画矩形
import fc.graphics.draw2d.AbstractDraw;
class fc.graphics.draw2d.Rect extends AbstractDraw {
private var _x:Number;
private var _y:Number;
private var _w:Number;
private var _h:Number;
public function Rect(x:Number, y:Number, w:Number, h:Number) {
_x = x;
_y = y;
_w = w;
_h = h;
}
public function draw() {
rectFunc(_x, _y, _w, _h);
}
//绘制矩形
private function rectFunc(x:Number, y:Number, w:Number, h:Number) {
_target.moveTo(x, y);
_target.lineTo(x+w, y);
_target.lineTo(x+w, y+h);
_target.lineTo(x, y+h);
_target.lineTo(x, y);
}
}
//画正多边
import fc.graphics.draw2d.AbstractDraw
class fc.graphics.draw2d.Poly extends AbstractDraw {
private var _x:Number;
private var _y:Number;
private var _r:Number;
private var _n:Number;
public function Poly(x:Number, y:Number, r:Number, n:Number) {
_x = x;
_y = y;
_r = r;
_n = n;
}
public function draw() {
polyFunc(_x, _y, _r, _n);
}
//绘制多边形
private function polyFunc(x:Number, y:Number, r:Number, n:Number) {
var pai = 2*Math.PI;
_target.moveTo(r+x, y);
for (var i = 0; i<pai/n*(n+1); i += pai/n) {
_target.lineTo(Math.cos(i)*r+x, Math.sin(i)*r+y);
}
}
}
//椭圆,圆
import fc.graphics.draw2d.AbstractDraw;
import fc.math.Degree;
class fc.graphics.draw2d.Ellipse extends AbstractDraw {
private var _x:Number;
private var _y:Number;
private var _rx:Number;
private var _ry:Number;
public function Ellipse(x:Number, y:Number, rx:Number, ry:Number) {
_x = x;
_y = y;
_rx = rx;
_ry = ry;
}
public function draw() {
ellipseFunc(_x, _y, _rx, _ry);
}
//绘制椭圆(圆)函数
public function ellipseFunc(x:Number, y:Number, rx:Number, ry:Number) {
if (ry == null) {
ry = rx;
}
var controlx = rx*(Math.SQRT2-1);
var controly = ry*(Math.SQRT2-1);
var anchorx = rx*Math.SQRT1_2;
var anchory = ry*Math.SQRT1_2;
_target.moveTo(x+rx, y);
_target.curveTo(x+rx, y+controly, x+anchorx, y+anchory);
_target.curveTo(x+controlx, y+ry, x, y+ry);
_target.curveTo(x-controlx, y+ry, x-anchorx, y+anchory);
_target.curveTo(x-rx, y+controly, x-rx, y);
_target.curveTo(x-rx, y-controly, x-anchorx, y-anchory);
_target.curveTo(x-controlx, y-ry, x, y-ry);
_target.curveTo(x+controlx, y-ry, x+anchorx, y-anchory);
_target.curveTo(x+rx, y-controly, x+rx, y);
}
}
//弧线
import fc.math.Degree;
import fc.graphics.draw2d.Ellipse;
//绘制椭圆或圆形曲线
class fc.graphics.draw2d.Arc extends Ellipse {
private var _staAngle;
private var _endAngle;
public function Arc(x:Number, y:Number, rx:Number, ry:Number, staAngle,
endAngle) {
_x = x;
_y = y;
_rx = rx;
_ry = ry;
_staAngle = staAngle;
_endAngle = endAngle;
}
public function draw() {
arcFunc(_x, _y, _rx, _ry, _staAngle, _endAngle);
}
//绘制椭圆(圆)形曲线
public function arcFunc(x, y, rx, ry, staAngle, endAngle) {
if (endAngle == staAngle) {
return;
}
if (Math.abs(endAngle-staAngle)>=360) {
ellipseFunc(x, y, rx, ry);
return;
}
if (ry == null) {
ry = rx;
}
var curAngle = staAngle;
var nextAngle = staAngle<endAngle ? (Math.floor(staAngle/45)+1)*45 :
(Math.ceil(staAngle/45)-1)*45;
var midcos, midAngle, controlx, controly, anchorx, anchory;
_target.moveTo(x+Degree.cosD(staAngle)*rx,
y+Degree.sinD(staAngle)*ry);
while (true) {
if (staAngle<endAngle && nextAngle>endAngle || staAngle>endAngle
&& nextAngle<endAngle) {
nextAngle = endAngle;
}
midcos = Degree.cosD((nextAngle-curAngle)/2);
midAngle = (nextAngle+curAngle)/2;
controlx = x+Degree.cosD(midAngle)/midcos*rx;
controly = y+Degree.sinD(midAngle)/midcos*ry;
anchorx = x+Degree.cosD(nextAngle)*rx;
anchory = y+Degree.sinD(nextAngle)*ry;
_target.curveTo(controlx, controly, anchorx, anchory);
if (nextAngle == endAngle) {
break;
}
curAngle = nextAngle;
nextAngle += staAngle<endAngle ? 45 : -45;
}
}
}
//扇形
import fc.math.Degree;
import fc.graphics.draw2d.Arc;
class fc.graphics.draw2d.Sector extends Arc {
public function Sector(x:Number, y:Number, rx:Number, ry:Number, staAngle,
endAngle) {
_x = x;
_y = y;
_rx = rx;
_ry = ry;
_staAngle = staAngle;
_endAngle = endAngle;
}
public function draw() {
sectorFunc(_x, _y, _rx, _ry, _staAngle, _endAngle);
}
//绘制扇形
private function sectorFunc(x, y, rx, ry, staAngle, endAngle) {
arcFunc(x, y, rx, ry, staAngle, endAngle);
if (Math.abs(staAngle-endAngle)>0 && Math.abs(staAngle-endAngle)<360)
{
_target.lineTo(x, y);
_target.lineTo(x+Degree.cosD(staAngle)*rx,
y+Degree.sinD(staAngle)*ry);
}
}
}
画图测试
//导入所需的类
import fc.graphics.*;
import fc.graphics.draw2d.*;
//创建绘图实例
var test:GDI = new GDI();
//定义绘图目标
test.target = this;
//定义钢笔样式
var pen = new Pen(5, Color.green, 100);
//定义填充样式
var brush = new SolidBrush(Color.green, 100);
//绘制填充椭圆
test.fill(brush, new Rect(100, 100, 100, 100));
//绘制直线
test.line(pen, new Line(0, 0, 100, 100));
//绘制多边形
test.line(pen, new Poly(200, 150, 200, 5));
//用一个按钮清除绘图
clearBtn.onRelease = function() {
test.clear();
};