开始新的blog之旅--flash3,0涂鸦板保存,撤销功能
一直以来都是在百度写的博客,今天不知道百度是哪抽风。居然把我的博客给封了,上贴吧去发泄一番,结果帖子都给删了。
不得不说你百度厉害,你自己犯的错误。让我们这些用户跟着受罪。我看今天很多博客都被封了,都不知道原因。提倡的用户体验呢?这也太差劲了吧。至少告诉我们哪里出现违反百度空间协议的文章或是文字了。太搞笑了。
算了,今天开始在这里写了,由于刚开始学习AS3.0这里就记录一些写过的代码和体会吧。
先发一个涂鸦板的代码咯。
有撤销和保存的功能。做撤销的时候想了半天。最后用数组实现了。用了matrix类. bitmapdata.draw方法。
package {
import fl.transitions.Tween;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import com.adobe.images.JPGEncoder;
import flash.filters.DropShadowFilter;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.ui.Mouse;
import flash.utils.ByteArray;
import flash.net.URLRequestMethod;
import fl.events.ColorPickerEvent;//组件颜色拾取器
import flash.display.BlendMode;
import flash.display.SimpleButton;
import fl.transitions.easing.*;
/**
* ...
* @author pangpang
*/
public class Painter extends MovieClip
{
private var _drawSprite:Sprite;
private var _ok:Boolean;
private var _lineArr:Array;
private var _count:uint;
private var _colorft:uint = 0xff00ff;
private var _colorbg:uint = 0xffffff;
private var _linestyle:Number = 2;
private var _alphastyle:Number = 1;
private var _fileName = "picSave";
private var _boardOpen:MovieClip;
private var _btmp:Bitmap;
public function Painter()
{
_lineArr = new Array();//存放数组
_drawSprite = new Sprite();//背景sprite
//scrollboard_mc.visible = false; //滚动面板可见
//增加背景图片
addParent(_colorbg, _drawSprite);
addMcEvent(_drawSprite);
}
/**
* 增加背景
*/
private function addParent(_color:uint,_parentMc:Sprite):void
{
_parentMc.graphics.lineStyle(2, 0x4E7EAD, 1);
_parentMc.graphics.beginFill(_color);
_parentMc.graphics.drawRoundRect(58, 51, 474, 328,5);
_parentMc.graphics.endFill();
addChild(_parentMc);
}
/**
* 增加画图背景
*/
private function addSprite(_color:uint,_parentMc:Sprite):void
{
var $sprite:Sprite = new Sprite();
$sprite.graphics.beginFill(_color,0);
$sprite.graphics.drawRoundRect(60, 53, 470, 324,5);
$sprite.graphics.endFill();
$sprite.width = 470;
$sprite.height = 324;
_parentMc.addChild($sprite);
$sprite.graphics.lineStyle(_linestyle, _colorft, _alphastyle);
$sprite.graphics.moveTo(mouseX, mouseY);
_lineArr.push($sprite);
}
/**
* 注册侦听器
*/
private function addMcEvent(_mc:Sprite):void
{
_mc.addEventListener(MouseEvent.MOUSE_DOWN, downHandle);
_mc.addEventListener(MouseEvent.MOUSE_UP, upHandle);
_mc.addEventListener(MouseEvent.MOUSE_MOVE, moveHandle);
//按钮注册侦听器
save_Btn.addEventListener(MouseEvent.MOUSE_DOWN,saveHandle);
clear_Btn.addEventListener(MouseEvent.MOUSE_DOWN,clearHandle);
reDo_Btn.addEventListener(MouseEvent.MOUSE_DOWN, reDoHandle);
//颜色拾取器注册侦听器
cp_front.addEventListener(ColorPickerEvent.CHANGE,frontHandle);
cp_bg.addEventListener(ColorPickerEvent.CHANGE, bgHandle);
//拖动按钮注册侦听器
line_btn.addEventListener(MouseEvent.MOUSE_DOWN, startDragLineHandle);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragLineHandle);
line_btn.addEventListener(MouseEvent.MOUSE_MOVE, moveDragLineHandle);
alpha_btn.addEventListener(MouseEvent.MOUSE_DOWN, startDragAlphaHandle);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragAlphaHandle);
alpha_btn.addEventListener(MouseEvent.MOUSE_MOVE, moveDragAlphaHandle);
//滚动条注册侦听器
//scrollBarboard_mc.addEventListener(MouseEvent.MOUSE_DOWN,openHandle);
}
/*
*滚动条函数
*/
private function openHandle(evt:MouseEvent):void
{
var $urlRequest:URLRequest = new URLRequest("shuoming.swf");
var $urlLoader:Loader = new Loader();
$urlLoader.load($urlRequest);
$urlLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandle);
}
private function loadHandle(evt:Event):void
{
var $loadContent:MovieClip = evt.target.content;
var $tween:Tween = new Tween($loadContent, "alpha", Elastic.easeOut, 0, 1, 2,true);
$loadContent.y = stage.stageHeight / 2-$loadContent.height/2;
$loadContent.x = stage.stageWidth / 2-$loadContent.width/2;
addChild($loadContent);
//_boardOpen = $loadContent
//$loadContent.btn_close.addEventListener(MouseEvent.MOUSE_DOWN,closeHanle);
}
private function closeHanle(evt:MouseEvent):void
{
//removeChild(_boardOpen);
}
/**
* 拖动按钮
*/
private function startDragLineHandle(evt:MouseEvent):void
{
line_btn.useHandCursor = true;
line_btn.startDrag(true, new Rectangle(106, 30, 60, 0));
}
private function stopDragLineHandle(evt:MouseEvent):void
{
line_btn.stopDrag();
}
private function moveDragLineHandle(evt:MouseEvent):void
{
_linestyle = int(((line_btn.x - 106) / 60) * 10);
}
private function startDragAlphaHandle(evt:MouseEvent):void
{
alpha_btn.startDrag(true, new Rectangle(226, 30, 60, 0));
alpha_btn.useHandCursor = true;
}
private function stopDragAlphaHandle(evt:MouseEvent):void
{
alpha_btn.stopDrag();
}
private function moveDragAlphaHandle(evt:MouseEvent):void
{
_alphastyle = 1-(alpha_btn.x - 226) / 60;
}
/**
* 颜色
*/
private function frontHandle(evt:ColorPickerEvent):void
{
_colorft = cp_front.selectedColor;
}
private function bgHandle(evt:ColorPickerEvent):void
{
_colorbg = cp_bg.selectedColor;
addParent(_colorbg, _drawSprite);//重置背景色
}
/**
* 处理函数
*/
private function downHandle(evt:MouseEvent):void
{
_ok = true;
addSprite(0xffffff, _drawSprite);
}
private function upHandle(evt:MouseEvent):void
{
_ok = false;
}
private function moveHandle(evt:MouseEvent):void
{
if(_ok){
_lineArr[_lineArr.length - 1].graphics.lineTo(mouseX, mouseY);
evt.updateAfterEvent();
}
}
/**
*
*按钮
*/
//保存
private function saveHandle(evt:MouseEvent):void
{
var _bt:BitmapData = new BitmapData(474, 328,false,0x000000);
for (var i:int = _drawSprite.numChildren; i > 0; i--)
{
_bt.draw(_lineArr[i-1], new Matrix(1, 0, 0, 1, -58, -51));
}
_btmp = new Bitmap(_bt);
var jpgEncoder:JPGEncoder = new JPGEncoder(90);
var bytes:ByteArray = jpgEncoder.encode(_bt);
var urlR:URLRequest = new URLRequest("http://127.0.0.1/readPic.asp?filename=picSave");
urlR.data = bytes;
urlR.method = URLRequestMethod.POST;
urlR.contentType = "application/octet-stream";
var urlL:URLLoader = new URLLoader();
urlL.load(urlR);
urlL.addEventListener(Event.COMPLETE,completeHandler);
}
private function completeHandler(evt:Event):void
{
addChild(_btmp);
_btmp.x = stage.stageWidth / 2 - _btmp.width / 2;
_btmp.y = stage.stageHeight / 2 - _btmp.height / 2;
var $filt:DropShadowFilter = new DropShadowFilter(0x000000, 50, 1, 1, 10, 10, 3, 1, false);
_btmp.filters.push($filt);
stage.addEventListener(MouseEvent.MOUSE_DOWN, closeBtHandle);
}
private function closeBtHandle(evt:MouseEvent):void
{
if (_btmp != null && this.contains(_btmp) )
{
removeChild(_btmp);
}
}
//清除graphics。重新生成
private function clearHandle(evt:MouseEvent):void
{
if (_drawSprite.numChildren >= 1)
{
for (var i:int = _drawSprite.numChildren; i >0; i--)
{
_drawSprite.removeChild(_lineArr[i-1]);
_lineArr.splice(i - 1, 1);
}
}
}
//撤销
private function reDoHandle(evt:MouseEvent):void
{
if (_drawSprite.numChildren >= 1)
{
_drawSprite.removeChild(_lineArr[_lineArr.length - 1]);
_lineArr.splice(_lineArr.length - 1, 1);
}
}
}
}
import fl.transitions.Tween;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import com.adobe.images.JPGEncoder;
import flash.filters.DropShadowFilter;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.ui.Mouse;
import flash.utils.ByteArray;
import flash.net.URLRequestMethod;
import fl.events.ColorPickerEvent;//组件颜色拾取器
import flash.display.BlendMode;
import flash.display.SimpleButton;
import fl.transitions.easing.*;
/**
* ...
* @author pangpang
*/
public class Painter extends MovieClip
{
private var _drawSprite:Sprite;
private var _ok:Boolean;
private var _lineArr:Array;
private var _count:uint;
private var _colorft:uint = 0xff00ff;
private var _colorbg:uint = 0xffffff;
private var _linestyle:Number = 2;
private var _alphastyle:Number = 1;
private var _fileName = "picSave";
private var _boardOpen:MovieClip;
private var _btmp:Bitmap;
public function Painter()
{
_lineArr = new Array();//存放数组
_drawSprite = new Sprite();//背景sprite
//scrollboard_mc.visible = false; //滚动面板可见
//增加背景图片
addParent(_colorbg, _drawSprite);
addMcEvent(_drawSprite);
}
/**
* 增加背景
*/
private function addParent(_color:uint,_parentMc:Sprite):void
{
_parentMc.graphics.lineStyle(2, 0x4E7EAD, 1);
_parentMc.graphics.beginFill(_color);
_parentMc.graphics.drawRoundRect(58, 51, 474, 328,5);
_parentMc.graphics.endFill();
addChild(_parentMc);
}
/**
* 增加画图背景
*/
private function addSprite(_color:uint,_parentMc:Sprite):void
{
var $sprite:Sprite = new Sprite();
$sprite.graphics.beginFill(_color,0);
$sprite.graphics.drawRoundRect(60, 53, 470, 324,5);
$sprite.graphics.endFill();
$sprite.width = 470;
$sprite.height = 324;
_parentMc.addChild($sprite);
$sprite.graphics.lineStyle(_linestyle, _colorft, _alphastyle);
$sprite.graphics.moveTo(mouseX, mouseY);
_lineArr.push($sprite);
}
/**
* 注册侦听器
*/
private function addMcEvent(_mc:Sprite):void
{
_mc.addEventListener(MouseEvent.MOUSE_DOWN, downHandle);
_mc.addEventListener(MouseEvent.MOUSE_UP, upHandle);
_mc.addEventListener(MouseEvent.MOUSE_MOVE, moveHandle);
//按钮注册侦听器
save_Btn.addEventListener(MouseEvent.MOUSE_DOWN,saveHandle);
clear_Btn.addEventListener(MouseEvent.MOUSE_DOWN,clearHandle);
reDo_Btn.addEventListener(MouseEvent.MOUSE_DOWN, reDoHandle);
//颜色拾取器注册侦听器
cp_front.addEventListener(ColorPickerEvent.CHANGE,frontHandle);
cp_bg.addEventListener(ColorPickerEvent.CHANGE, bgHandle);
//拖动按钮注册侦听器
line_btn.addEventListener(MouseEvent.MOUSE_DOWN, startDragLineHandle);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragLineHandle);
line_btn.addEventListener(MouseEvent.MOUSE_MOVE, moveDragLineHandle);
alpha_btn.addEventListener(MouseEvent.MOUSE_DOWN, startDragAlphaHandle);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragAlphaHandle);
alpha_btn.addEventListener(MouseEvent.MOUSE_MOVE, moveDragAlphaHandle);
//滚动条注册侦听器
//scrollBarboard_mc.addEventListener(MouseEvent.MOUSE_DOWN,openHandle);
}
/*
*滚动条函数
*/
private function openHandle(evt:MouseEvent):void
{
var $urlRequest:URLRequest = new URLRequest("shuoming.swf");
var $urlLoader:Loader = new Loader();
$urlLoader.load($urlRequest);
$urlLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandle);
}
private function loadHandle(evt:Event):void
{
var $loadContent:MovieClip = evt.target.content;
var $tween:Tween = new Tween($loadContent, "alpha", Elastic.easeOut, 0, 1, 2,true);
$loadContent.y = stage.stageHeight / 2-$loadContent.height/2;
$loadContent.x = stage.stageWidth / 2-$loadContent.width/2;
addChild($loadContent);
//_boardOpen = $loadContent
//$loadContent.btn_close.addEventListener(MouseEvent.MOUSE_DOWN,closeHanle);
}
private function closeHanle(evt:MouseEvent):void
{
//removeChild(_boardOpen);
}
/**
* 拖动按钮
*/
private function startDragLineHandle(evt:MouseEvent):void
{
line_btn.useHandCursor = true;
line_btn.startDrag(true, new Rectangle(106, 30, 60, 0));
}
private function stopDragLineHandle(evt:MouseEvent):void
{
line_btn.stopDrag();
}
private function moveDragLineHandle(evt:MouseEvent):void
{
_linestyle = int(((line_btn.x - 106) / 60) * 10);
}
private function startDragAlphaHandle(evt:MouseEvent):void
{
alpha_btn.startDrag(true, new Rectangle(226, 30, 60, 0));
alpha_btn.useHandCursor = true;
}
private function stopDragAlphaHandle(evt:MouseEvent):void
{
alpha_btn.stopDrag();
}
private function moveDragAlphaHandle(evt:MouseEvent):void
{
_alphastyle = 1-(alpha_btn.x - 226) / 60;
}
/**
* 颜色
*/
private function frontHandle(evt:ColorPickerEvent):void
{
_colorft = cp_front.selectedColor;
}
private function bgHandle(evt:ColorPickerEvent):void
{
_colorbg = cp_bg.selectedColor;
addParent(_colorbg, _drawSprite);//重置背景色
}
/**
* 处理函数
*/
private function downHandle(evt:MouseEvent):void
{
_ok = true;
addSprite(0xffffff, _drawSprite);
}
private function upHandle(evt:MouseEvent):void
{
_ok = false;
}
private function moveHandle(evt:MouseEvent):void
{
if(_ok){
_lineArr[_lineArr.length - 1].graphics.lineTo(mouseX, mouseY);
evt.updateAfterEvent();
}
}
/**
*
*按钮
*/
//保存
private function saveHandle(evt:MouseEvent):void
{
var _bt:BitmapData = new BitmapData(474, 328,false,0x000000);
for (var i:int = _drawSprite.numChildren; i > 0; i--)
{
_bt.draw(_lineArr[i-1], new Matrix(1, 0, 0, 1, -58, -51));
}
_btmp = new Bitmap(_bt);
var jpgEncoder:JPGEncoder = new JPGEncoder(90);
var bytes:ByteArray = jpgEncoder.encode(_bt);
var urlR:URLRequest = new URLRequest("http://127.0.0.1/readPic.asp?filename=picSave");
urlR.data = bytes;
urlR.method = URLRequestMethod.POST;
urlR.contentType = "application/octet-stream";
var urlL:URLLoader = new URLLoader();
urlL.load(urlR);
urlL.addEventListener(Event.COMPLETE,completeHandler);
}
private function completeHandler(evt:Event):void
{
addChild(_btmp);
_btmp.x = stage.stageWidth / 2 - _btmp.width / 2;
_btmp.y = stage.stageHeight / 2 - _btmp.height / 2;
var $filt:DropShadowFilter = new DropShadowFilter(0x000000, 50, 1, 1, 10, 10, 3, 1, false);
_btmp.filters.push($filt);
stage.addEventListener(MouseEvent.MOUSE_DOWN, closeBtHandle);
}
private function closeBtHandle(evt:MouseEvent):void
{
if (_btmp != null && this.contains(_btmp) )
{
removeChild(_btmp);
}
}
//清除graphics。重新生成
private function clearHandle(evt:MouseEvent):void
{
if (_drawSprite.numChildren >= 1)
{
for (var i:int = _drawSprite.numChildren; i >0; i--)
{
_drawSprite.removeChild(_lineArr[i-1]);
_lineArr.splice(i - 1, 1);
}
}
}
//撤销
private function reDoHandle(evt:MouseEvent):void
{
if (_drawSprite.numChildren >= 1)
{
_drawSprite.removeChild(_lineArr[_lineArr.length - 1]);
_lineArr.splice(_lineArr.length - 1, 1);
}
}
}
}
会饮酒时为伴侣,再逢天下平.