3 Replies Latest reply on Oct 25, 2010 9:23 AM by Flex harUI

    Troubles with pipeline and handling events

    mikall1979

      Hi All,
      I need some help to understand why my code doesn't work properly.

       

      I have an image and I apply on it some trasformations.

       

      Eg.
      If I click on "rotate" button, the image rotates,

      If I click on "crop" button, I cut the image,

      and so on.


      Now the question.


      I print on screen after every click the size of the actual image state, but the size shown correspond to the size of the previous state!

       

      Could someone explain me why?


      Below the code:

       

      <?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" minWidth="955" minHeight="600">


         


          <fx:Script>


              <![CDATA[


                  private function rotate(img:SWFLoader, angolo:Number):Bitmap


                  {               


                      var sourceBD:Bitmap = Bitmap(img.content);                                               


                      var m:Matrix = new Matrix();


                      m.rotate(angolo);


                      var targetBD:BitmapData = new BitmapData(img.content.width, img.content.height);                   


                      targetBD.draw(sourceBD, m);               


                      var bitmap:Bitmap = new Bitmap(targetBD);


                      return bitmap;


                  }


                  private function crop(img:SWFLoader, startP:Point, endP:Point):Bitmap


                  {


                      var sourceBD:BitmapData = Bitmap(img.content).bitmapData;


                      var targetBD:BitmapData = new BitmapData(endP.x - startP.x, endP.y - startP.y);


                      var rect:Rectangle = new Rectangle(startP.x, startP.y, endP.x, endP.y);


                      targetBD.copyPixels(sourceBD, rect, new Point(0,0));                   


                      var bitmap:Bitmap = new Bitmap(targetBD);                       


                      return bitmap;


                  }


                  private function zoom(img:SWFLoader,width:int, height:int):Bitmap{


                      var m:Matrix = new Matrix();


                      m.scale(width/img.width, height/img.height);


                      var sourceBD:Bitmap = Bitmap(img.content);


                      var targetBD:BitmapData = new BitmapData(width, height);


                      targetBD.draw(sourceBD, m);               


                      var bitmap:Bitmap = new Bitmap(targetBD);


                      return bitmap;


                  }


                 


                  private function onBtLoadClick1(evt:Event):void {


                      imageTest.source=rotate(imageTest,0.02);


                      X.text = imageTest.width.toString();


                      Y.text = imageTest.height.toString();


                  }


                  private function onBtLoadClick2(evt:Event):void {


                      imageTest.source=zoom(imageTest,300, 400);


                      X.text = imageTest.width.toString();


                      Y.text = imageTest.height.toString();


                  }


                  private function onBtLoadClick3(evt:Event):void {


                      imageTest.source=crop(imageTest,new Point(50,50), new Point(100,100));


                      X.text = imageTest.width.toString();


                      Y.text = imageTest.height.toString();


                  }


                 


              ]]>


          </fx:Script>


          <mx:SWFLoader id="imageTest" x="69" y="93" source="van.jpg"/>


          <s:Button x="183" y="38" label="Rotate" click="onBtLoadClick1(event)"/>


          <s:Button x="360" y="37" label="Resize" click="onBtLoadClick2(event)"/>


          <s:Button x="272" y="38" label="Crop" click="onBtLoadClick3(event)"/>


          <s:Label x="461" y="93" text="Label" id="X"/>


          <s:Label x="461" y="122" text="Label" id="Y"/>


      </s:Application>

       

      Thank you in advance.

      Michele