Fl030_Main.as (Main)

Fl030_Main.as
package {
    import flash.display.Sprite;
    import com.borealkiss.controls.ViewController;
    import com.borealkiss.controls.HatchedEllipseController;
    
    [SWF(width="550",height="400",backgroundColor="0x0")]
    public class Fl030_Main extends Sprite{
        public function Fl030_Main(){
            var canvas:Sprite = new Sprite();
            this.addChild(canvas);
            
            var controller:ViewController = new HatchedEllipseController(canvas);
            controller.draw();
        }
    }
}

com.borealkiss.controls.ViewController.as (Abstract)

ViewController.as
/**
 * @date 21/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.controls{
    import flash.display.DisplayObjectContainer;
    import flash.errors.IllegalOperationError;
    
    /**
     * Abstract class
     */ 
    public class ViewController{
        protected var _target:DisplayObjectContainer;
        
        /**
         * Constructor
         */     
        public function ViewController(target:DisplayObjectContainer){
            _target = target;
            
            if (Object(this) == ViewController){
                throw new IllegalOperationError(
                    "Abstract class must be inherited in a subclass");
            }
        }
        
        public function draw():void{
        }
    }
}

com.borealkiss.controls.HatchedEllipseController.as

HatchedEllipseController.as
/**
 * @date 21/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.controls{
    import com.borealkiss.controls.ViewController;
    import flash.display.DisplayObjectContainer;
    import com.borealkiss.display.HatchedEllipse;
    
    public class HatchedEllipseController extends ViewController{
        protected const STAGE_WIDTH:Number = 550;
        protected const STAGE_HEIGHT:Number = 400;
        protected const PADDING_X:Number = 10;
        protected const PADDING_Y:Number = 10;
        protected const SPAN_X:Number = 10;
        protected const SPAN_Y:Number = 10;
        protected const LIMIT_X:uint = 10;
        protected const LIMIT_Y:uint = 10;
        protected const WIDTH:Number = (STAGE_WIDTH - 2*PADDING_X)/LIMIT_X - SPAN_X;
        protected const HEIGHT:Number = (STAGE_HEIGHT - 2*PADDING_Y)/LIMIT_Y - SPAN_Y;
        
        /**
         * Constructor
         */ 
        public function HatchedEllipseController(target:DisplayObjectContainer){
            super(target);
        }
        
        override public function draw():void{            
            for (var i:int=0; i<LIMIT_X; i++){
                for (var j:int=0; j<LIMIT_Y; j++){
                    var color:uint = this.getColor(i,j);
                    var radii:Array = this.getRadii(i,j);
                    var rect:HatchedEllipse = new HatchedEllipse(radii[0],radii[1],radii[0],
                                                                this.getColor(i,j),0x0);
                    rect.x = PADDING_X + (2*i + 1)*(WIDTH/2 + SPAN_X/2);
                    rect.y = PADDING_Y + (2*j + 1)*(HEIGHT/2 + SPAN_Y/2);
                    _target.addChild(rect);
                }
            }
        }
        
        protected function getRadii(xNum:int,yNum:int):Array{
            var radMax:Number = Math.min(WIDTH/2,HEIGHT/2);
            var radMin:Number = radMax/2;
            var radX:Number = (radMin - radMax)*xNum/(LIMIT_X - 1) + radMax;
            var radY:Number = (radMin - radMax)*yNum/(LIMIT_Y - 1) + radMax;
            return [radX,radY];
        }
        
        protected function getColor(xNum:int,yNum:int):uint{
            var r:int = 0;
            var g:int = 255*(0.4*(LIMIT_X - 1 - xNum)/LIMIT_X + 
                             0.4*(LIMIT_Y - 1 - yNum)/LIMIT_Y + 0.2);
            var b:int = g;
            return r << 16 | g << 8 | b;
        }
    }
}

com.borealkiss.display.HatchedLine.as

HatchedEllipse.as
/**
 * @date 21/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display{
    import flash.display.Sprite;
    import flash.display.Graphics;
    
    public class HatchedEllipse extends Sprite{
        protected var _radiusX:Number;
        protected var _radiusY:Number;
        protected var _random:uint;
        protected var _stroke:uint;
        protected var _fillColor:uint;
        
        //Must be tuned
        protected const STEPS:uint = 360;
        protected const ALPHA:Number = 0.2;
        
        /**
         * Constructor
         */ 
        public function HatchedEllipse(radiusX:Number,radiusY:Number,random:uint,
                                        stroke:uint=0x0,fillColor:uint=0x0){
            _radiusX = radiusX;
            _radiusY = radiusY;
            _random = random;
            _stroke = stroke;
            _fillColor = fillColor;
            this.init();
        }
        
        protected function init():void{
            this.update();
        }
        
        protected function update():void{
            var g:Graphics = this.graphics;
            g.clear();
            
            g.beginFill(_fillColor,ALPHA);
            g.drawEllipse(-(_radiusX),-(_radiusY),_radiusX*2,_radiusY*2);
            g.endFill();
            
            g.lineStyle(0,_stroke,ALPHA);
            for (var t:int=0; t<STEPS; t++){
                var theta:Number = Math.PI*t/180;
                var xstart:Number = _radiusX*Math.cos(theta);
                var ystart:Number = _radiusY*Math.sin(theta);
                var hatchRad:Number = _random*Math.random();
                //So that the hatch goes outer.
                var hatchTheta:Number = theta + randomGen(90)*Math.PI/180;
                var xend:Number = xstart + hatchRad*Math.cos(hatchTheta);
                var yend:Number = ystart + hatchRad*Math.sin(hatchTheta);
                g.moveTo(xstart,ystart);
                g.lineTo(xend,yend);
            }
        }
        
        /**
         * @return Random values between -random and random.
         */ 
        protected function randomGen(random:uint):Number{
            return 2*random*(Math.random() - 1/2);
        }
    }
}
Powered by boreal-kiss.com 2008.