鼠标拖动画直线

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" >
<fx:Script>
<![CDATA[
import flash.events.MouseEvent;

import mx.binding.utils.BindingUtils;
import mx.core.UIComponent;

private var mX:Number;
private var mY:Number;
private var IsDown:Boolean=false;
private var sprite:Sprite;
private var uic:UIComponent;

public var lineSize:int;
[Bindable]
public var lineColor:int=0xff1100;

private function DrawLine():void{

paper.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
paper.addEventListener(MouseEvent.MOUSE_MOVE,movelineHandler);
paper.addEventListener(MouseEvent.MOUSE_UP,upHandler);
BindingUtils.bindProperty(this,"lineColor",cp,"selectedColor");

}
private function movelineHandler(e:MouseEvent):void{
if(IsDown){
var x:Number=paper.mouseX;
var y:Number=paper.mouseY;

paper.contentGroup.graphics.clear();
paper.contentGroup.graphics.lineStyle(lineSize,lineColor,1);

paper.contentGroup.graphics.moveTo(mX,mY);
paper.contentGroup.graphics.lineTo(x,y);

}
}

private function downHandler(e:MouseEvent):void{
IsDown=true;

mX=paper.mouseX;
mY=paper.mouseY;
}

private function upHandler(e:MouseEvent):void{
IsDown=false;
}

private function moveHandler(e:MouseEvent):void{
if(IsDown){

var x:Number = paper.mouseX;
var y:Number = paper.mouseY;
paper.contentGroup.graphics.clear();
paper.contentGroup.graphics.lineStyle(lineSize,lineColor,1);
paper.contentGroup.graphics.moveTo(mX,mY);
paper.contentGroup.graphics.lineTo(x,y);
mX=x;
mY=y;

}
}

private function doChange():void{
lineSize=int(sel.value);

}

]]>
</fx:Script>
<s:Panel title="test" width="894" height="537">
<s:Panel width="743" x="0" y="38" height="462" title="记事板" fontFamily="中易宋体">
<s:BorderContainer id="paper" width="741" height="429" x="0" contentBackgroundColor="#FFFFFF" chromeColor="#120101" y="0">
</s:BorderContainer>
</s:Panel>

<mx:ColorPicker x="138" y="10" width="23" height="24" id="cp" selectedColor="{lineColor}"/>

<s:NumericStepper x="169" y="10" id="sel" change="doChange()"/>
<s:Button x="252" y="9" label="直线" id="line" click="DrawLine()"/>
</s:Panel>
</s:Application>

posted @ 2014-03-24 21:27  Brook Legend  阅读(506)  评论(0编辑  收藏  举报