用AS3做最简单的绘图版
步骤:
画板->图形
var 画板:Sprite;
画板.drawRect;//画背景
var 图形:Shape;
画板.addChild(图形);
画板监听鼠标事件
在事件监听器里让图形去绘制
图形.graphics.moveTo(this.mouseX, this.mouseY);
code:
1 package
2 {
3 import flash.display.Shape;
4 import flash.display.Sprite;
5 import flash.events.MouseEvent;
6 /**
7 * ...
8 * @author sange
9 */
10 public class DrawingBoard extends Sprite
11 {
12 private var mySprite:Sprite;
13 private var myShape:Shape;
14 public function DrawingBoard()
15 {
16 init();
17 }
18
19 private function init():void
20 {
21 this.mySprite = new Sprite();
22 addChild(this.mySprite);
23 this.mySprite.graphics.beginFill(0x336699);
24 this.mySprite.graphics.drawRect(0, 0, 550, 400);
25 this.mySprite.graphics.endFill();
26 this.mySprite.addEventListener(MouseEvent.MOUSE_DOWN, mySpriteMouseDownHandler);
27 this.mySprite.addEventListener(MouseEvent.MOUSE_UP, mySpriteMouseUpHandler);
28
29 this.myShape = new Shape();
30 mySprite.addChild(myShape);
31 }
32 // 鼠标按下事件
33 private function mySpriteMouseDownHandler(event:MouseEvent):void
34 {
35 this.myShape.graphics.lineStyle(1, 0x00FF00, 1);
36 this.myShape.graphics.moveTo(this.mouseX, this.mouseY);
37 this.mySprite.addEventListener(MouseEvent.MOUSE_MOVE, mySpriteMouseMoveHandler);
38 }
39
40 // 鼠标离开事件
41 private function mySpriteMouseUpHandler(event:MouseEvent):void
42 {
43 this.mySprite.removeEventListener(MouseEvent.MOUSE_MOVE, mySpriteMouseMoveHandler);
44 }
45
46 // 移动事件
47 private function mySpriteMouseMoveHandler(event:MouseEvent):void
48 {
49 this.myShape.graphics.lineTo(mouseX, mouseY);
50 }
51 }
52 }
2 {
3 import flash.display.Shape;
4 import flash.display.Sprite;
5 import flash.events.MouseEvent;
6 /**
7 * ...
8 * @author sange
9 */
10 public class DrawingBoard extends Sprite
11 {
12 private var mySprite:Sprite;
13 private var myShape:Shape;
14 public function DrawingBoard()
15 {
16 init();
17 }
18
19 private function init():void
20 {
21 this.mySprite = new Sprite();
22 addChild(this.mySprite);
23 this.mySprite.graphics.beginFill(0x336699);
24 this.mySprite.graphics.drawRect(0, 0, 550, 400);
25 this.mySprite.graphics.endFill();
26 this.mySprite.addEventListener(MouseEvent.MOUSE_DOWN, mySpriteMouseDownHandler);
27 this.mySprite.addEventListener(MouseEvent.MOUSE_UP, mySpriteMouseUpHandler);
28
29 this.myShape = new Shape();
30 mySprite.addChild(myShape);
31 }
32 // 鼠标按下事件
33 private function mySpriteMouseDownHandler(event:MouseEvent):void
34 {
35 this.myShape.graphics.lineStyle(1, 0x00FF00, 1);
36 this.myShape.graphics.moveTo(this.mouseX, this.mouseY);
37 this.mySprite.addEventListener(MouseEvent.MOUSE_MOVE, mySpriteMouseMoveHandler);
38 }
39
40 // 鼠标离开事件
41 private function mySpriteMouseUpHandler(event:MouseEvent):void
42 {
43 this.mySprite.removeEventListener(MouseEvent.MOUSE_MOVE, mySpriteMouseMoveHandler);
44 }
45
46 // 移动事件
47 private function mySpriteMouseMoveHandler(event:MouseEvent):void
48 {
49 this.myShape.graphics.lineTo(mouseX, mouseY);
50 }
51 }
52 }