Une horloge avec des aiguilles des plus sommaires, pas de quoi être fier mais ça peut toujours être utile.
package site.draw
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.utils.Timer;
public class AnalogicClock extends Sprite
{
protected var __hands:Sprite;
protected var __width:Number = 50;
protected var __height:Number = 50;
protected var __center:Point;
protected var __d:Date;
protected var __timer:Timer;
public function AnalogicClock()
{
__hands = new Sprite();
__hands.rotation = -90;
addChild(__hands);
__center = new Point(.5 * __width, .5 * __height);
__timer = new Timer(500, 0);
__timer.addEventListener(TimerEvent.TIMER, tickHandler);
addEventListener(Event.ADDED_TO_STAGE, init);
addEventListener(Event.REMOVED_FROM_STAGE, dispose);
}
protected function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
running = true;
}
protected function dispose(e:Event):void
{
removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
}
override public function set width(width:Number):void
{
__width = width;
__center.x = __hands.x = .5 * __width;
draw();
}
override public function set height(height:Number):void
{
__height = height;
__center.y = __hands.y = .5 * __height;
draw();
}
public function set running(running:Boolean):void
{
if (running != __timer.running)
running ? __timer.start() : __timer.stop();
}
public function get running():Boolean
{
return __timer.running;
}
public function setTime(d:Date):void
{
running = false;
__d = d;
draw();
}
private function tickHandler(e:TimerEvent = null):void
{
__d = new Date();
draw();
}
protected function draw():void
{
__hands.graphics.clear();
var minuteRatio:Number = __d.getMinutes() / 60,
hourRatio:Number = ((__d.getHours() + minuteRatio) % 12) / 12,
secondsRatio:Number = __d.getSeconds() / 60;
drawHand(hourRatio, 2, 0xff0000, 1, .7);
drawHand(minuteRatio, 2, 0x0);
drawHand(secondsRatio, 1, 0x0);
}
protected function drawHand(thetaRatio:Number, thickness:Number, color:uint, alpha: Number = 1, lengthRatio:Number = 1):void
{
var theta:Number = 2 * Math.PI * thetaRatio;
__hands.graphics.lineStyle(thickness, color, alpha);
__hands.graphics.moveTo(0, 0);
__hands.graphics.lineTo(Math.cos(theta) * __center.x * lengthRatio, Math.sin(theta) * __center.y * lengthRatio);
}
}
}