可视化时钟,好吧!我承认我懒

http://bbs.9ria.com/thread-39644-1-1.html

  1. package
  2. {
  3.         import flash.display.Shape;
  4.         import flash.display.Sprite;
  5.         import flash.events.TimerEvent;
  6.         import flash.utils.Timer;
  7.         
  8.         public class Clock extends Sprite
  9.         {
  10.                 private var r:Number;//钟面半径
  11.         private var hourHand:Shape;//时针
  12.         private var minuteHand:Shape;//分针
  13.         private var secondHand:Shape;//秒针
  14.         
  15.         public function Clock(r:Number)
  16.         {
  17.                 this.r = r;
  18.             initClock();
  19.             initClockPoint();
  20.             initClockFinger();
  21.             
  22.             var timer:Timer = new Timer(1000,0);
  23.             timer.addEventListener(TimerEvent.TIMER, refreshClock);
  24.             timer.start();
  25.         }
  26.         
  27.         //画钟面
  28.         public function initClock():void
  29.         {
  30.             this.graphics.lineStyle(2);
  31.             this.graphics.drawCircle(r,r,r+2);
  32.             this.graphics.beginFill(0xffffff);
  33.             this.graphics.drawCircle(r,r,r);
  34.             this.graphics.beginFill(0x000000);
  35.             this.graphics.drawCircle(r,r,1);
  36.         }
  37.         
  38.         //画整点
  39.         public function initClockPoint():void
  40.         {
  41.             for (var i:int=0; i<12; i++)
  42.             {
  43.                 var px:Number = Math.sin(Math.PI*i/6)*(r-5);
  44.                 var py:Number = Math.cos(Math.PI*i/6)*(r-5);
  45.                 this.graphics.beginFill(0x000000);
  46.                 var rr:int = 1;
  47.                 if (i%3 == 0)
  48.                         rr = 2;
  49.                 this.graphics.drawCircle(r+px, r-py, rr);
  50.             }
  51.         }
  52.         
  53.         //初始化指针
  54.         public function initClockFinger():void
  55.         {
  56.             hourHand = new Shape();
  57.                         hourHand.graphics.lineStyle(3);
  58.                 hourHand.graphics.moveTo(0, -0.5*r);
  59.                 hourHand.graphics.lineTo(0, 0);
  60.                         hourHand.x = r;
  61.                 hourHand.y = r;                
  62.                 addChild(hourHand);
  63.                                      
  64.                   minuteHand = new Shape();
  65.                         minuteHand.graphics.lineStyle(2);
  66.                 minuteHand.graphics.moveTo(0, -0.6*r);
  67.                 minuteHand.graphics.lineTo(0, 0);
  68.                  minuteHand.x = r;
  69.                 minuteHand.y = r;
  70.                         addChild(minuteHand);

  71.                  secondHand = new Shape();
  72.                         secondHand.graphics.lineStyle(1);
  73.                 secondHand.graphics.moveTo(0, -0.8*r);
  74.                 secondHand.graphics.lineTo(0, 0);
  75.                         secondHand.x = r;
  76.                 secondHand.y = r;
  77.                 addChild(secondHand);
  78.         }
  79.         
  80.         //刷新指针
  81.         public function refreshClock(event:TimerEvent):void
  82.         {
  83.                 var currentTime:Date = new Date();
  84.                         var hour:uint = currentTime.getHours();
  85.                         var minute:uint = currentTime.getMinutes();
  86.                         var second:uint = currentTime.getSeconds();
  87.                     
  88.                 this.hourHand.rotation = hour*30 + minute*0.5;
  89.                 this.minuteHand.rotation = minute*6;
  90.                 this.secondHand.rotation = second*6;
  91.         }
  92.     }
  93. }
复制代码
  1. package {
  2.     import flash.display.Sprite;
  3.     
  4.     public class demoAs1 extends Sprite
  5.     {
  6.         public function demoAs1()
  7.         {
  8.             var clock:Clock = new Clock(50);
  9.             clock.x = 200;
  10.             clock.y = 100;
  11.             this.addChild(clock);
  12.         }
  13.     }
  14. }
复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值