Mega Code Archive

 
Categories / Flex / Graphics
 

Use Image control to load a draggable image into a Canvas container

<!-- Code from Flex 4 Documentation "Using Adobe Flex 4". This user guide is licensed for use under the terms of the Creative Commons Attribution  Non-Commercial 3.0 License.  This License allows users to copy, distribute, and transmit the user guide for noncommercial  purposes only so long as    (1) proper attribution to Adobe is given as the owner of the user guide; and    (2) any reuse or distribution of the user guide contains a notice that use of the user guide is governed by these terms.  The best way to provide notice is to include the following link.  To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ -->     <!-- dragdrop\DandDImage.mxml --> <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>                    //Import classes so you don't have to use full names.          import mx.managers.DragManager;          import mx.core.DragSource;          import mx.events.DragEvent;          import flash.events.MouseEvent;          // Embed icon image.          [Embed(source='a.jpg')]          public var globeImage:Class;          // The mouseMove event handler for the Image control          // initiates the drag-and-drop operation.          private function mouseMoveHandler(event:MouseEvent):void          {              var dragInitiator:Image=Image(event.currentTarget);              var ds:DragSource = new DragSource();              ds.addData(dragInitiator, "img");              DragManager.doDrag(dragInitiator, ds, event);          }          // The dragEnter event handler for the Canvas container          // enables dropping.          private function dragEnterHandler(event:DragEvent):void {              if (event.dragSource.hasFormat("img"))              {                  DragManager.acceptDragDrop(Canvas(event.currentTarget));              }          }          // The dragDrop event handler for the Canvas container          // sets the Image control's position by          // "dropping" it in its new location.          private function dragDropHandler(event:DragEvent):void {              Image(event.dragInitiator).x = Canvas(event.currentTarget).mouseX;              Image(event.dragInitiator).y = Canvas(event.currentTarget).mouseY;          }             </fx:Script>     <!-- The Canvas is the drag target -->     <mx:Canvas id="v1" width="500" height="500" borderStyle="solid"         backgroundColor="#DDDDDD" dragEnter="dragEnterHandler(event);"         dragDrop="dragDropHandler(event);">         <!-- The image is the drag initiator. -->         <mx:Image id="myimg" source="@Embed(source='a.jpg')"             mouseMove="mouseMoveHandler(event);" />     </mx:Canvas> </s:Application>