topical media & game development

talk show tell print

#graphic-flex-draw-classes-FillingShapesWithBitmaps.ax

#graphic-flex-draw-classes-FillingShapesWithBitmaps.ax [swf] [flash] flex


  package {
  
          import flash.display.BitmapData;
          import flash.display.Shape;
          import flash.display.Sprite;
          import flash.geom.Matrix;
          import flash.events.*;
  
          [SWF(width=550, height=400, backgroundColor=0xFFFFFF)]
  
          
Demonstrates the different parameters of beginBitmapFill().

  
          public class @ax-graphic-flex-draw-classes-FillingShapesWithBitmaps extends Sprite {
  
                  
Constructor. Calls draw().

  
                  public function @ax-graphic-flex-draw-classes-FillingShapesWithBitmaps() {
                          super();
                          this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
                  }
                  
                  private function onAddedToStage(e:Event):void
                  {
                          this.init();
                  }
                  
                  private function init():void
                  {
                          draw();
                  }
  
                  
Creates an image using Perlin noise.
returns: The image created.

  
                  private function makeBitmapData():BitmapData {
                          var bitmap:BitmapData = new BitmapData(100, 100);
                          bitmap.perlinNoise(40, 40, 2, Math.random(), true, false);
                          return bitmap;
                  }
  
                  
Draws the created image into four shapes.

  
                  private function draw():void {
                          var bitmap:BitmapData = makeBitmapData();
  
                          // parameters for shapes
                          var width:Number = stage.stageWidth/2;
                          var height:Number = stage.stageHeight/2;
                          var radius:Number = 70;
  
                          // top left shape simply draws bitmap
                          var shape:Shape = new Shape();
                          with (shape.graphics) {
                                  beginBitmapFill(bitmap);
                                  drawRoundRect(0, 0, width, height, radius, radius);
                                  endFill();
                          }
                          addChild(shape);
  
                          // top right shape does not tile bitmap
                          shape = new Shape();
                          var matrix:Matrix = new Matrix();
                          matrix.translate((width-bitmap.width)/2, (height-bitmap.height)/2);
                          with (shape.graphics) {
                                  beginBitmapFill(bitmap, matrix, false);
                                  drawRoundRect(0, 0, width, height, radius, radius);
                                  endFill();
                          }
                          shape.x = width;
                          addChild(shape);
                          
                          // bottom left shape shows rotated bitmap, not tiled
                          shape = new Shape();
                          matrix.rotate(Math.PI/4);
                          with (shape.graphics) {
                                  beginBitmapFill(bitmap, matrix, false);
                                  drawRoundRect(0, 0, width, height, radius, radius);
                                  endFill();
                          }
                          shape.y = height;
                          addChild(shape);
  
                          // bottom right shape shows scaled bitmap, not smoothed
                          shape = new Shape();
                          matrix = new Matrix();
                          matrix.scale(10, 10);
                          with (shape.graphics) {
                                  beginBitmapFill(bitmap, matrix, false, false);
                                  drawRoundRect(0, 0, width, height, radius, radius);
                                  endFill();
                          }
                          shape.x = width;
                          shape.y = height;
                          addChild(shape);
                  }
  
          }
  
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.