图形的绘制
Graphics类提供了相关的方法:
1,清空画布
graphics.clear();
2,设置画笔
graphics.lineStyle(thickness:Number = NaN, color:uint = 0, alpha:Number = 1.0, pixelHinting:Boolean = false, scaleMode:String = "normal", caps:String = null, joints:String = null, miterLimit:Number = 3):void
设置画笔后,就可以使用当前设置的画笔粗细,颜色,透明度等绘制图形,直到再次调用lineStyle方法改变画笔设置。
3,设置画笔起点
graphics.moveTo(startX,startY);
参数为横纵坐标
4,绘制直线
graphics.lineTo(endX,endY);
参数为直线终点横纵坐标
5,绘制矩形
graphics.drawRect(startX,startY,w,h);
参数分别为,开始横坐标,开始纵坐标,宽度,高度
6,绘制椭圆:
graphics.drawEllipse(startX,startY,w,h);
参数分别为:起点横坐标,起点纵坐标,宽度,高度
7,绘制三角形
我在as3中没有找到绘制三角形的方法,可以绘制收尾相连的三条直线代替
private function draw(point1:Point,point2:Point,point3:Point):void
{
this.graphics.moveTo(point1.x,point1.y);
this.graphics.lineTo(point2.x,point2.y);
this.graphics.lineTo(point3.x,point3.y);
this.graphics.lineTo(point1.x,point1.y);
}
{
this.graphics.moveTo(point1.x,point1.y);
this.graphics.lineTo(point2.x,point2.y);
this.graphics.lineTo(point3.x,point3.y);
this.graphics.lineTo(point1.x,point1.y);
}
8,绘制不规则多边形
其实就是很多很多小直线连成的
private function drawPolygon(pointArray:Array):void
{ graphics.moveTo(Point(this.pointArray[0]).x,Point(this.pointArray[0]).y);
for(var i:Number=1;i<this.pointArray.length;i++)
{
graphics.lineTo(Point(this.pointArray[i]).x,Point(this.pointArray[i]).y);
}
graphics.moveTo(Point(this.pointArray[0]).x,Point(this.pointArray[0]).y);
}
{ graphics.moveTo(Point(this.pointArray[0]).x,Point(this.pointArray[0]).y);
for(var i:Number=1;i<this.pointArray.length;i++)
{
graphics.lineTo(Point(this.pointArray[i]).x,Point(this.pointArray[i]).y);
}
graphics.moveTo(Point(this.pointArray[0]).x,Point(this.pointArray[0]).y);
}
数组里存放的是一系列鼠标经过的点。
9,填充封闭图形
graphics.beginFill(this.fillColor,this.fillAlpha);
this.drawPolygon(this.pointArray);//something like this
graphics.endFill();
this.drawPolygon(this.pointArray);//something like this
graphics.endFill();
beginFill方法的前两个参数是填充颜色和透明度
beginFill和endFill方法之间必须有一个使用画笔绘制的封闭区域,可以是圆,矩形等,也可以是多条直线收尾相接形成的封闭图形。
注:1,坐标位置都是相对于父容器的
2,所有继承Sprite的类都有一个属性graphics用来得到一个Graphics的实例。