// 文件 1.DTimer.as
package{
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.text.StaticText;
import flash.events.*;
import flash.text.TextField;
import flash.text.TextFormat;
public class DTimer extends Sprite{
// 設(shè)置時(shí)鐘寬度
public var w:uint=200;
// 設(shè)置時(shí)鐘高度
public var h:uint=200;
// 設(shè)置時(shí)鐘半徑
public var radius:uint;
// 設(shè)置時(shí)鐘中心位置
public var centerX:int;
public var centerY:int;
// 定義三個(gè)形狀表示時(shí)鐘指針
public var hourHand:Shape;
public var minuteHand:Shape;
public var secondHand:Shape;
// 定義時(shí)鐘樣式
public var bgColor:uint=0xeeeeff;
public var hourHandColor:uint=0x003366;
public var minuteHandColor:uint=0x000099;
public var secondHandColor:uint=0xcc0033;
public var x0:uint;
public var y0:uint;
public var x1:uint;
public var y1:uint;
public var txt:TextField;
// 定義時(shí)間
public var currentTime:Date;
public function init():void{
// 繪制表盤
drawBorder();
// 繪制表盤顯示文字
drawLabels();
// 繪制指針
createHands();
Drawlss();
// 顯示文本
CreateTxt();
}
public function DTimer(w:uint){
this.w=w;
this.h=w; // 是圓盤所以取一樣
this.radius=Math.round(this.w/2);
this.centerX=this.radius;
this.centerY=this.radius;
}
public function drawBorder(){
graphics.lineStyle(0.5,0x999999);
graphics.beginFill(bgColor);
graphics.drawCircle(centerX,centerY,radius);
graphics.endFill();
}
public function drawLabels(){
for(var i:Number=1;i<=12;i++){
var label:TextField=new TextField();
label.text=i.toString();
// 小時(shí)位置
var angleInRadians:Number=i*30*(Math.PI/180);
/* 度*π
弧度==---------
180
弧度*180
度==------------
π
即:π弧度等于180度
*/
label.x=centerX+(0.9*radius*Math.sin(angleInRadians))-5;
label.y=centerY-(0.9*radius*Math.cos(angleInRadians))-9;
trace(Math.sin(angleInRadians));
// 格式化文本內(nèi)容
var tf:TextFormat=new TextFormat();
tf.font="Arial";
tf.bold="true";
tf.size=12;
label.setTextFormat(tf);
addChild(label);
}
}
// 繪制分鐘對應(yīng)位置
public function Drawlss(){
for(var i:Number=0;i<=60;i++){
var lshape:Shape=new Shape();
Drawls(lshape,i*6,0x000000,1);
addChild(lshape);
}
}
// 繪制分針方法
public function Drawls(hand:Shape,rot:uint,color:uint,thickness:Number):void{
var angleInRadius:Number=rot*(Math.PI/180);
x1=centerX+(radius*Math.sin(angleInRadius));
y1=centerY-(radius*Math.cos(angleInRadius));
if(rot%30==0){
x0=centerX+(0.95*radius*Math.sin(angleInRadius));
y0=centerY-(0.95*radius*Math.cos(angleInRadius));
}else{
x0=centerX+(0.97*radius*Math.sin(angleInRadius));
y0=centerY-(0.97*radius*Math.cos(angleInRadius));
}
hand.graphics.lineStyle(thickness,color);
hand.graphics.moveTo(x0,y0);
hand.graphics.lineTo(x1,y1);
}
public function createHands():void{
var hourHandShape:Shape=new Shape();
drawHand(hourHandShape,Math.round(radius*0.5),hourHandColor,3.0);
this.hourHand=Shape(addChild(hourHandShape));
this.hourHand.x=centerX;
this.hourHand.y=centerY;
// 繪制分針
var minuteHandShape:Shape=new Shape();
drawHand(minuteHandShape,Math.round(radius*0.8),minuteHandColor,2.0);
this.minuteHand=Shape(addChild(minuteHandShape));
this.minuteHand.x=centerX;
this.minuteHand.y=centerY;
// 繪制秒針
var secondHandShape:Shape=new Shape();
drawHand(secondHandShape,Math.round(radius*0.9),secondHandColor,0.5);
this.secondHand=Shape(addChild(secondHandShape));
this.secondHand.x=centerX;
this.secondHand.y=centerY;
}
public function drawHand(hand:Shape,distance:uint,color:uint,thickness:Number):void{
hand.graphics.lineStyle(thickness,color);
hand.graphics.moveTo(0,distance);
hand.graphics.lineTo(0,0);
}
public function draw():void{
currentTime=new Date();
showTime(currentTime);
showT(currentTime);
}
public function CreateTxt():void{
txt=new TextField();
txt.width=300;
txt.x=centerX-radius;
txt.y=centerY-radius-20;
addChild(txt);
}
public function showT(time:Date){
var seconds:uint=time.getSeconds();
var minutes:uint=time.getMinutes();
var hours:uint=time.getHours();
var year:uint=time.getFullYear();
var month:uint=time.getMonth()+1;
var date:uint=time.getDate();
this.txt.text="現(xiàn)在時(shí)間是"+year+"年"+month+"月"+date+"日"+hours+"時(shí)"+minutes+"分"+seconds+"秒";
}
public function showTime(time:Date):void{
var seconds:uint=time.getSeconds();
var minutes:uint=time.getMinutes();
var hours:uint=time.getHours();
this.secondHand.rotation=180+(seconds*6);
this.minuteHand.rotation=180+(minutes*6);
this.hourHand.rotation=180+(hours*30)+(minutes*0.5);
}
}
}
//文件2. SimpleClock.as
package{
import flash.display.Sprite;
import DTimer;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class SimpleClock extends Sprite{
public var face:DTimer;
public var ticker:Timer;
public static const millisecondsPerMinute:int=1000*60;
public static const millisecondsPerHour:int=1000*60*60;
public static const millisecondsPerDay:int=1000*60*60*24;
public function initClock(faceSize:Number=200){
var invoiceDate:Date=new Date();
var millisecondsPerDay:int=1000*60*60*24;
var dueDate:Date =new Date(invoiceDate.getTime()+(30*millisecondsPerDay));
var oneHourFromNow:Date=new Date();
oneHourFromNow.setTime(oneHourFromNow.getTime()+millisecondsPerHour);
// 創(chuàng)建表盤,初始化參數(shù)
face=new DTimer(Math.max(20,faceSize));
face.init();
addChild(face);
face.draw();
// 創(chuàng)建Timer對象
ticker=new Timer(1000);
ticker.addEventListener(TimerEvent.TIMER,onTick);
ticker.start();
}
public function onTick(event:TimerEvent){
face.draw();
}
}
}
// 文件3.DateTimer.as
package{
import flash.display.Sprite;
import DTimer;
import SimpleClock;
public class DateTimer extends Sprite{
public function DateTimer(){
var sc:SimpleClock=new SimpleClock();
sc.x=165;
sc.y=100;
addChild(sc);
sc.initClock();
}
}
}