0 Replies Latest reply on Nov 9, 2011 10:31 PM by flexprabu

    Can't get correct (x1,y1,x2,y2)values in Draw Line between two points.

    flexprabu

      Hi fridends,

         

                  am trying to get correct value in draw line between two points and i have create some mistake in my code but i cant find that any one tel me how can i get correct x1,y1 and x2,y2 value in my out put of the program.i give the full source code and out put  below.

       

      Main Application


      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application frameRate="250" creationComplete="init()" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" borderStyle="solid" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#FFFFFF, #DFDFDF]">

                <mx:Script>

                          <![CDATA[

                                    import mx.controls.Alert;

                                    import san_comps.lineHandle;

                                    import san_comps.LineTool;

                                    import mx.core.Container;

                              private var xpos:int;

                              private var ypos:int;

                              private var h:int;

                              private var w:int;

                              private var rad:int;

                                    private function onMouseDown(event:MouseEvent):void

                                    {

                                              xpos=circleHolder.mouseX;

                                              ypos=circleHolder.mouseY;

                                              circleHolder.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);

                                              circleHolder.addEventListener(MouseEvent.MOUSE_MOVE,onlineMove);

                                              circleHolder.addEventListener(MouseEvent.MOUSE_UP,onLineUp);

                                    }

                                    private function onlineMove(event:MouseEvent):void

                                    {

                                              xposn1=(event.currentTarget as Container).mouseX;

                                              yposn1=(event.currentTarget as Container).mouseY;

                                              circleHolder.graphics.clear();

                                              circleHolder.graphics.lineStyle(2,cPkr.selectedColor);

                                              circleHolder.graphics.moveTo(xpos,ypos);

                                              circleHolder.graphics.lineTo(xposn1,yposn1);

                                    }

                                    private var gLine:LineTool;

                                    private var xposn1:int;

                                    private var yposn1:int;

                                    private function onLineUp(event:MouseEvent):void

                                    {

                                              circleHolder.removeEventListener(MouseEvent.MOUSE_MOVE,onlineMove);

                                              circleHolder.removeEventListener(MouseEvent.MOUSE_UP,onLineUp);

                                              circleHolder.graphics.clear();

                                              xposn1=circleHolder.mouseX;

                                              yposn1=circleHolder.mouseY;

                                              var a:int;

                                                var b:int;

                                                var rx:int;

                                                var ry:int;

                                              if(xposn1>xpos)

                                                {

                                                          a=xposn1-xpos;

                                                          rx=xpos;

                                                }

                                                else

                                                {

                                                          a=xpos-xposn1;

                                                          rx=xposn1;

                                                }

                                                if(yposn1>ypos)

                                                {

                                                          b=yposn1-ypos;

                                                          ry=ypos;

                                                }

                                                else

                                                {

                                                          b=ypos-yposn1;

                                                          ry=yposn1;

                                                }

                                              gLine=new LineTool();

                                              gLine.x1=0;

                                              gLine.y1=0;

                                              gLine.x2=uint(xposn1)-uint(xpos);

                                              gLine.y2=uint(yposn1)-uint(ypos);

                                              gLine.a=a;

                                              gLine.b=b;

                                              gLine.thickness=lt.value;

                                              gLine.color=cPkr.selectedColor;

                                              circleHolder.addChild(gLine);

                                              gLine.x=xpos;

                                              gLine.y=ypos;

                                              gLine.name="line";

                                              gLine.addEventListener(MouseEvent.CLICK,getCurrentObj);

                                              if(obj is LineTool)

                                              {

                                                        LineTool(obj).showHandle=false;

                                              }

                                              obj=gLine;

                                              LineTool(obj).showHandle=true;

                                    }

                                    private function load(x1:int,y1:int,x2:int,y2:int,xval:int,yval:int):void

                                    {

                                              gLine=new LineTool();

                                              gLine.x1=0;

                                              gLine.y1=0;

                                              gLine.x2=uint(x2)-uint(x1);

                                              gLine.y2=uint(y2)-uint(y1);

                                              gLine.thickness=lt.value;

                                              gLine.color=cPkr.selectedColor;

                                              circleHolder.addChild(gLine);

                                              gLine.x=xval;

                                              gLine.y=yval;

                                              gLine.name="line";

                                              gLine.addEventListener(MouseEvent.CLICK,getCurrentObj);

                                              if(obj is LineTool)

                                              {

                                                        LineTool(obj).showHandle=false;

                                              }

                                              obj=gLine;

                                              LineTool(obj).showHandle=true;

       

                                    }

                                    private var obj:Object;

                                    private function getCurrentObj(event:MouseEvent):void

                                    {

                                              if(LineTool(obj).showHandle==true)

                                              {

                                                        LineTool(obj).showHandle=false;

                                              }

                                              obj=event.currentTarget as LineTool;

                                              LineTool(obj).showHandle=true;

                                    }

                                    public function remove(event:Event):void

                                    {

                                              if(obj.name=='line')

                                              {

                                                        circleHolder.removeChild(obj as LineTool);

                                              }

                                    }

                                    public var str:String;

                                    public function addXML():void

                                    {

                                      str=obj.getLine();

                                      Txm.text=str;

                                    }

                                    public function valueXY(event:MouseEvent):void

                  {

                                              Xva.text=(event.currentTarget as Canvas).mouseX.toString();

                                              Yva.text=(event.currentTarget as Canvas).mouseY.toString();

                                    }

                          ]]>

                </mx:Script>

       

                <mx:Canvas mouseMove="valueXY(event)" id="circleHolder" dropShadowEnabled="true" width="100%" height="600" borderStyle="solid" borderColor="#646566" x="0" y="0">

                </mx:Canvas>

       

                <mx:ColorPicker close="obj.color=cPkr.selectedColor;obj.update()" id="cPkr" labelField="Color"  x="40" y="608" selectedColor="#000000"/>

                <mx:Button click="circleHolder.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown)" x="427" y="608" label="Line" toolTip="Click And Draw Line"/>

                <mx:Button click="load(obj.x1,obj.y1,obj.x2,obj.y2,obj.x,obj.y)" x="484" y="608" label="Copy"/>

                <mx:Button click="remove(event)" label="Delete" x="546" y="608"/>

                <mx:NumericStepper value="1" minimum="1" maximum="5" change="obj.thickness=lt.value;obj.update();" id="lt" x="170" y="608"/>

                <mx:Button click="addXML()"  x="618" y="608" label="XML"/>

                <mx:Button x="101" y="651" label="Clear" click="Txm.text=&quot; &quot;"/>

                <mx:TextArea  x="101" y="680"  width="840" height="63" id="Txm" color="#0B406D" fontWeight="bold" editable="false"/>

                <mx:Label x="40" y="696" text="XML:" fontWeight="bold" fontSize="17"/>

                <mx:HBox x="246" y="608" width="165" height="23" verticalAlign="middle" horizontalGap="5" borderStyle="solid" backgroundColor="#C0BCBC" horizontalAlign="center">

                          <mx:Text text="X:" fontWeight="bold" fontSize="14" color="#151618"/>

                          <mx:Text fontWeight="bold" fontSize="13" id="Xva" width="40" color="#0A239F"/>

                          <mx:Text text="Y:" fontWeight="bold" fontSize="14" color="#151618"/>

                          <mx:Text fontWeight="bold" fontSize="13" id="Yva" width="38" color="#0A239F"/>

       

                </mx:HBox>

                <mx:Label x="70" y="610" text="Line Thickness:" fontWeight="bold"/>

                <mx:Label x="-1" y="610" text="Color:" fontWeight="bold"/>

      </mx:Application>

       

      LineTool.as


      package com

      {

                import flash.display.Sprite;

                import flash.events.MouseEvent;

       

                import mx.core.UIComponent;

                public class LineTool extends UIComponent

                {

                          public var x1:int;

                          public var x2:int;

                          public var y1:int;

                          public var y2:int;

                          public var color:uint;

                          public var thickness:int;

                          public var a:int;

                            public var b:int;

                          private var h1:lineHandle;

                          private var h2:lineHandle;

                          private var shape:Sprite;

       

                          public function set showHandle(val:Boolean):void

                          {

                                    h1.visible=val;

                                    h2.visible=val;

                          }

                          public function get showHandle():Boolean

                          {

                                    return h1.visible;

                          }

       

                          private function startMove(event:MouseEvent):void

                          {

                                    this.startDrag();

                          }

                          private function stopMove(event:MouseEvent):void

                          {

                                    this.stopDrag();

                          }

                          public function LineTool()

                          {

                                    super();

                                    width=(x1+x2>8)?x1+x2:8;

                                    height=(y1+y2>8)?y1+y2:8;

                                    h1=new lineHandle();

       

                                    h1.name="h1";

                                    h1.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);

                                    h1.x=x1;

                                    h1.y=y1;

                                      addChild(h1);

                                    h2=new lineHandle();

                                    h2.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);

                                    h2.x=x2;

                                    h2.y=y2;

                                    h2.name="h2";

                                    addChild(h2);

                                    /* if(h1.x<h2.x)

                                    {

                                              x1=h1.x+h1.width/2;

                                              x2=h2.x;

                                    }

                                    else

                                    {

                                              x1=h1.x;

                                              x2=h2.x+h2.width/2;

                                    } */

                                    x1=h1.x+h1.width/2;

                                    x2=h2.x+h2.width/2;

                                    y1=h1.y+h1.height/2;

                                    y2=h2.y+h2.height/2;

                                    drawLine1(x1,y1,x2,y2,color,thickness);

                                    showHandle=false;

                          }

                           override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void

                          {

                                    super.updateDisplayList(unscaledWidth,unscaledHeight);

                                    h1.x=x1;

                                    h1.y=y1;

                                    h2.x=x2;

                                    h2.y=y2;

                                    /* if(h1.x<h2.x)

                                    {

                                              x1=h1.x+h1.width;

                                              x2=h2.x;

                                    }

                                    else

                                    {

                                              x1=h1.x;

                                              x2=h2.x+h2.width;

                                    } */

                                    x1=h1.x+h1.width/2;

                                    x2=h2.x+h2.width/2;

                                    y1=h1.y+h1.height/2;

                                    y2=h2.y+h2.height/2;

                                    drawLine(x1,y1,x2,y2,color,thickness);

       

                          }

                          private function drawLine(x1:int,y1:int,x2:int,y2:int,color:uint,t:int):void

                          {

                                    /* if(h1.x<h2.x)

                                    {

                                              x1=h1.x+h1.width;

                                              x2=h2.x;

                                    }

                                    else

                                    {

                                              x1=h1.x;

                                              x2=h2.x+h2.width;

                                    } */

                                    x1=h1.x+h1.width/2;

                                    x2=h2.x+h2.width/2;

                                    y1=h1.y+h1.height/2;

                                    y2=h2.y+h2.height/2;

                                    shape.graphics.clear();

                                    shape.graphics.lineStyle(t,color);

                                    shape.graphics.moveTo(x1,y1);

                                    shape.graphics.lineTo(x2,y2);

                          }

                          public function update():void

                          {

                                    drawLine(x1,y1,x2,y2,color,thickness);

                          }

                          private function drawLine1(x1:int,y1:int,x2:int,y2:int,color:uint,t:int):void

                          {

                                    shape=new Sprite();

                                    shape.addEventListener(MouseEvent.MOUSE_DOWN,startMove);

                                    shape.addEventListener(MouseEvent.MOUSE_UP,stopMove);

                                     shape.buttonMode=true;

                                    shape.useHandCursor=true;

                                    shape.graphics.clear();

                                    shape.graphics.lineStyle(t,color);

                                    /* h2.x=x2;

                                    h2.y=y2; */

       

                                    shape.graphics.moveTo(x1,y1);

                                    shape.graphics.lineTo(x2,y2);

                                    this.addChildAt(shape,0);

                          }

                          private function onMouseDown(event:MouseEvent):void

                          {

                                    (event.currentTarget as lineHandle).startDrag();

                                    if(event.currentTarget.name=="h1")

                                    {

       

                                              h1.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);

                                              h1.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);

                                    }

                                    else

                                    {

                                              h2.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);

                                              h2.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);

                                    }

                          }

                          private function onMouseMove(event:MouseEvent):void

                          {

                                    /* if(h1.x<h2.x)

                                    {

                                              x1=h1.x+h1.width;

                                              x2=h2.x;

                                    }

                                    else

                                    {

                                              x1=h1.x;

                                              x2=h2.x+h2.width;

                                    } */

                                    x1=h1.x+h1.width/2;

                                    x2=h2.x+h2.width/2;

                                    y1=h1.y+h1.height/2;

                                    y2=h2.y+h2.height/2;

                                    drawLine(x1,y1,x2,y2,color,thickness);

                          }

                          /* private function onMouseMove1(event:MouseEvent):void

                          {

                                    x1=h1.x;

                                    y1=h1.y;

                                    x2=h2.x;

                                    y2=h2.y;

                                    drawLine(x1,y1,x2,y2,color,thickness);

                          } */

                          private function onMouseUp(event:MouseEvent):void

                          {

                                    /* if(h1.x<h2.x)

                                    {

                                              x1=h1.x+h1.width;

                                              x2=h2.x;

                                    }

                                    else

                                    {

                                              x1=h1.x;

                                              x2=h2.x+h2.width;

                                    } */

                                    x1=h1.x+h1.width/2;

                                    x2=h2.x+h2.width/2;

                                    y1=h1.y+h1.height/2;

                                    y2=h2.y+h2.height/2;

                                    drawLine(x1,y1,x2,y2,color,thickness);

                                    (event.currentTarget as lineHandle).stopDrag();

                                    if(event.currentTarget.name=="h1")

                                    {

                                              h1.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);

                                              h1.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);

                                    }

                                    else

                                    {

                                              h2.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);

                                              h2.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);

                                    }

       

                          }

                          public function getLine():XML

                          {

                                    var result:XML=new XML(<root/>);

                                    result.line.@x1=uint(x);

                                    result.line.@x2=uint(x2);

                                    result.line.@y1=uint(y);

                                    result.line.@y2=uint(y2);

                                    result.line.@fx=x;

                                    result.line.@fy=y;

                                    result.line.@id=this.id;

                                    result.line.@style="stroke:#"+color+";"+"stroke-width:"+thickness;

                                    //trace("result:"+result.toString());

                                    return result;

                          }

       

                }

      }

       

      lineHandle.mxml

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Button buttonMode="true" useHandCursor="true" toolTip="Drag to resize Line" xmlns:mx="http://www.adobe.com/2006/mxml" borderColor="#363636" cornerRadius="0" width="8" height="8">

       

      </mx:Button>

       

      OutPut

       

      Line.png