Improved clock

Source: Internet
Author: User
Tags addchild cos current time sin
  

The instance clock in Flash 9 is improved for class files, and the dial subdivision and Chinese display time are added.

Mainly modifiedAnalogClockFace class. The modified code is as follows:

Package com. example. programmingas3.clock {
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;

/**
* Displays a round clock face with an hour hand, a minute hand, and a second hand.
*/
Public class AnalogClockFace extends Sprite {
/**
* The desired width of this component, as opposed to the. width
* Property which represents tha actual width.
*/
Public var w: U int = 200;

/**
* The desired height of this component, as opposed to the. height
* Property which represents tha actual height.
*/
Public var h: uint = 200;

/**
* The radius from the center of the clock to
* Outer edge of the circular face outline.
*/
Public var radius: uint;

/**
* The coordinates of the center of the face.
*/
Public var centerX: int;
Public var centerY: int;

/**
* The three hands of the clock.
*/
Public var hourHand: Shape;
Public var minuteHand: Shape;
Public var secondHand: Shape;

/**
* The colors of the background and each hand.
* These cocould be set using parameters or
* Styles in the future.
*/
Public var bgColor: uint = 0 xEEEEFF;
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;
/**
* Stores a snapshot of the current time, so it
* Doesn't change while in the middle of drawing
* Three hands.
*/
Public var currentTime: Date;

/**
* Contructs a new clock face. The width and
* Height will be equal.
*/
Public function AnalogClockFace (w: uint ){
This. w = w;
This. h = w;

// Rounds to the nearest pixel
This. radius = Math. round (this. w/2 );

// The face is always square now, so
// Distance to the center is the same
// Horizontally and vertically
This. centerX = this. radius;
This. centerY = this. radius;
  }
/**
* Creates the outline, hour labels, and clock hands.
*/
Public function init (): void {
// Draws the circular clock outline
DrawBorder ();

// Draws the hour numbers
DrawLabels ();

// Creates the three clock hands
CreateHands ();
Drawlss ();
CreatTxt ();
  }
/**
* Draws a circular border.
*/
Public function drawBorder (): void {
Graphics. lineStyle (0.5, 0x999999 );
Graphics. beginFill (bgColor );
Graphics. drawCircle (centerX, centerY, radius );
Graphics. endFill ();
  }
/**
* Puts numeric labels at the hour points.
*/
Public function drawLabels (): void {
For (var I: Number = 1; I <= 12; I ++ ){
// Creates a new TextField showing the hour number
Var label: TextField = new TextField ();
Label. text = I. toString ();

// Places hour labels around the clock face.
// The sin () and cos () functions both operate on angles in radians.
Var angleInRadians: Number = I * 30 * (Math. PI/180 );
// Place the label using the sin () and cos () functions to get the x, y coordinates.
// The multiplier value of 0.9 puts the labels inside the outline of the clock face.
// The integer value at the end of the equation adjusts the label position,
// Since the x, y coordinate is in the upper left corner.
Label. x = centerX + (0.9 * radius * Math. sin (angleInRadians)-5;
Label. y = centerY-(0.9 * radius * Math. cos (angleInRadians)-9;

// Formats the label text.
Var tf: TextFormat = new TextFormat ();
Tf. font = "Arial ";
Tf. bold = "true ";
Tf. size = 12;
Label. setTextFormat (tf );

// Adds the label to the clock face display list.
AddChild (label );
   }
  }
Public function Drawlss (){
For (var I: Number = 0; I <= 60; I ++ ){
Var lshape: Shape = new Shape ();
// Trace (I)
Drawls (lshape, I * x );
AddChild (lshape );
   }
  }
Public function Drawls (hand: Shape, rot: uint, color: uint, thickness: Number): void {
Var angleInRadians: Number = rot * (Math. PI/180 );
X1 = centerX + (radius * Math. sin (angleInRadians ));
Y1 = centerY-(radius * Math. cos (angleInRadians ));
If (rot % 30 = 0 ){

X0 = centerX + (0.95 * radius * Math. sin (angleInRadians ));
Y0 = centerY-(0.95 * radius * Math. cos (angleInRadians ));
} Else {
X0 = centerX + (0.97 * radius * Math. sin (angleInRadians ));
Y0 = centerY-(0.97 * radius * Math. cos (angleInRadians ));
   }
Hand. graphics. lineStyle (thickness, color );
Hand. graphics. moveTo (x0, y0 );
Hand. graphics. lineTo (x1, y1 );
  }
/**
* Creates hour, minute, and second hands using the 2D drawing API.
*/
Public function createHands (): void {
// Uses a Shape since it's the simplest component that supports
// The 2D drawing API.
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;
  }
/**
* Draws a clock hand with a given size, color, and thickness.
*/
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 );
  }
/**
* Called by the parent container when the display is being drawn.
*/
Public function draw (): void {
// Stores the current date and time in an instance variable
CurrentTime = new Date ();
ShowTime (currentTime );
ShowT (currentTime );
  }
Public function CreatTxt (): void {
Txt = new TextField ();
Txt. width = 300;
// Label. text =;
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 = "the current time is: "+ year +" year "+ month +" month "+ date +" day "+ hours +" hour "+ minutes +" minute "+ seconds +" second ";
// SetTxt (datetxt)
  }
/**
* Displays the given Date/Time in that good old analog clock style.
*/

Public function showTime (time: Date): void {
// Gets the time values
Var seconds: uint = time. getSeconds ();
Var minutes: uint = time. getMinutes ();
Var hours: uint = time. getHours ();
// Multiplies by 6 to get degrees
This. secondHand. rotation = 180 + (seconds * 6 );
This. minuteHand. rotation = 180 + (minutes * 6 );

// Multiplies by 30 to get basic degrees, and then
// Adds up to 29.5 degrees (59*0.5) to account
// For the minutes
This. hourHand. rotation = 180 + (hours * 30) + (minutes * 0.5 );
  }
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.