Shape类
所在的包:flash.display
所在的类:public class Shape
通过 ActionScript 绘图应用程序编程接口 (API),可使用 Sprite 类创建简单形状。 Sprite 类包括 graphics属性,该属性使您可以从 Graphics 类访问方法。
Sprite 类也包括 graphics 属性,并且它包括不可用于 Sprite类的其它功能。 例如,Sprite 对象是显示对象容器,而 Sprite对象不是(并且不能包含子显示对象)。 由于此原因,Sprite对象会比包含相同图形的 Sprite 对象消耗的内存少。 但是,Sprite 对象支持鼠标单击事件,而 Sprite对象不支持。
公共属性:graphics : Graphics[只读 (read-only)] 指定属于该 Shape对象的 Graphics 对象,可通过此对象执行矢量绘画命令。
公共方法Shape(); 创建新的 Shape对象。
package {
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.JointStyle;
import flash.display.LineScaleMode;
import flash.display.Shape;
import flash.display.Sprite;
public class ShapeExample extends Sprite {
private var size:uint = 80;
private var bgColor:uint = 0xFFCC00;
private var borderColor:uint = 0x666666;
private var borderSize:uint = 0;
private var cornerRadius:uint = 9;
private var gutter:uint = 5;
public function ShapeExample() {
doDrawCircle();
doDrawRoundRect();
doDrawRect();
refreshLayout();
}
private function refreshLayout():void {
var ln:uint = numChildren;
var child:DisplayObject;
var lastChild:DisplayObject = getChildAt(0);
lastChild.x = gutter;
lastChild.y = gutter;
for (var i:uint = 1; i < ln; i++) {
child = getChildAt(i);
child.x = gutter + lastChild.x + lastChild.width;
child.y = gutter;
lastChild = child;
}
}
private function doDrawCircle():void {
var child:Shape = new Shape();
var halfSize:uint = Math.round(size/2);
child.graphics.beginFill(bgColor);
child.graphics.lineStyle(borderSize, borderColor);
child.graphics.drawCircle(halfSize, halfSize, halfSize);
child.graphics.endFill();
addChild(child);
}
private function doDrawRoundRect():void {
var child:Shape = new Shape();
child.graphics.beginFill(bgColor);
child.graphics.lineStyle(borderSize, borderColor);
child.graphics.drawRoundRect(0, 0, size, size, cornerRadius);
child.graphics.endFill();
addChild(child);
}
private function doDrawRect():void {
var child:Shape = new Shape();
child.graphics.beginFill(bgColor);
child.graphics.lineStyle(borderSize, borderColor);
child.graphics.drawRect(0, 0, size, size);
child.graphics.endFill();
addChild(child);
}
}
}
以上代码是:
- 声明
size属性以备日后在确定每个形状的大小时使用。 - 声明若干属性,通过这些属性将背景色设置为橙色,将边框颜色设置为深灰色,将边框大小设置为 0 个像素,将边角半径设置为 9 个像素,并将舞台边缘与其它对象之间的间隔设置为 5 个像素。
- 使用在前一步骤中声明的属性以及 Graphics 类的内置方法在坐标(x = 0,y = 0)处绘制圆形、圆角矩形和正方形。
- 通过使用
refreshLayout()方法,沿舞台顶部重新绘制每个形状,起点为 x = 5,y = 5,各形状之间的间隔为 5 个像素。
浙公网安备 33010602011771号