/*临时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();
}
} |