Aclock.h

Go to the documentation of this file.
00001 
00002 ///////////////////////////////////////////////////////////////////
00003 //  ROOT implementation of the X11 xclock.
00004 //
00005 //  To run this example do the following:
00006 //  $ root
00007 //  root [0] gSystem.Load("Aclock")
00008 //  root [1] Aclock a
00009 //  <enjoy>
00010 //  root [2] .q
00011 //
00012 //  Other ROOT fun examples: Tetris, Hello ...
00013 ///////////////////////////////////////////////////////////////////
00014 
00015 #ifndef ACLOCK_H
00016 #define ACLOCK_H
00017 
00018 #include <TTimer.h>
00019 #include <TCanvas.h>
00020 #include <TPolyLine.h>
00021 #include <TDatime.h>
00022 #include <TPoints.h>
00023 #include <TMath.h>
00024 #include <TList.h>
00025 
00026 class TPolygon : public TPolyLine {
00027 
00028 protected:
00029    TPad  *fPad;
00030 
00031 public:
00032    TPolygon(Int_t n, Float_t *x, Float_t *y);
00033    virtual ~TPolygon() { fPad->GetListOfPrimitives()->Remove(this); }
00034 
00035    virtual void Paint(Option_t *option="");
00036 
00037    TPad  *GetPad() { return fPad; }
00038 };
00039 
00040 
00041 
00042 class ClockPoints : public TPoints {
00043 
00044 public:
00045    ClockPoints(Coord_t x=0, Coord_t y=0) : TPoints(x,y) { }
00046    ~ClockPoints() { }
00047 
00048    void SetXY(Coord_t x, Coord_t y) { SetX(x); SetY(y); }
00049 
00050    void Rotate(Float_t clock_angle)  // Rotates the coordinate system a clock_angle degrees clockwise
00051    {
00052       const float deg2rad = .017453292519943295769F;  // pi/180
00053 
00054       Float_t rX, rY;
00055       Float_t angle = clock_angle*deg2rad;     // clock_angle to angle in radians
00056 
00057       rX = GetX()*TMath::Cos(angle)+GetY()*TMath::Sin(angle);
00058       rY = GetY()*TMath::Cos(angle)-GetX()*TMath::Sin(angle);
00059       SetXY(rX,rY);
00060    }
00061 
00062    void Scale(Float_t factorX, Float_t factorY) { SetX(GetX()*factorX); SetY(GetY()*factorY); }
00063    void Shift(Coord_t x, Coord_t y) { SetX(GetX()+x); SetY(GetY()+y); }
00064 };
00065 
00066 
00067 
00068 class ClockHand : public TPolygon {
00069 
00070 protected:
00071    UInt_t   fPrevTimeValue;    // used during updating
00072    Float_t *fX0;         // initial shape of clock hand corresponds to 00:00:00
00073    Float_t *fY0;         // initial shape of clock hand corresponds to 00:00:00
00074 
00075    static TDatime *fgTime;           // current date/time
00076 
00077    void  Move(Float_t angle);        // rotate initial shape to angle
00078    virtual UInt_t   GetTimeValue() { return GetMinute(); } // could be overloaded
00079    virtual Float_t  GetHandAngle() { return 0; }           // must be overloaded
00080 
00081 public:
00082    ClockHand(Int_t n, Float_t *x, Float_t *y);
00083    virtual ~ClockHand() { }
00084 
00085    UInt_t GetTime()    { fgTime->Set(); return fgTime->GetTime(); }
00086    UInt_t GetHour()    { return  GetTime()/10000; }
00087    UInt_t GetMinute()  { return (GetTime()%10000)/100; }
00088    UInt_t GetSecond()  { return (GetTime()%100); }
00089 
00090    void   Update();
00091 
00092    Bool_t IsModified() { return (fPrevTimeValue != GetTimeValue()); }
00093 };
00094 
00095 
00096 
00097 class MinuteHand : public ClockHand {
00098 
00099 private:
00100    static Float_t fgMinuteHandX[];
00101    static Float_t fgMinuteHandY[];
00102 
00103 public:
00104    MinuteHand(Int_t n=3, Float_t *x=fgMinuteHandX, Float_t *y=fgMinuteHandY)
00105       : ClockHand(n,x,y) { }
00106    ~MinuteHand() { }
00107 
00108    Float_t GetHandAngle() { return 6.*(GetMinute()+ GetSecond()/60.); }
00109 };
00110 
00111 
00112 
00113 class HourHand : public ClockHand {
00114 
00115 private:
00116    static Float_t fgHourHandX[];
00117    static Float_t fgHourHandY[];
00118 
00119 public:
00120    HourHand(Int_t n=3, Float_t *x=fgHourHandX, Float_t *y=fgHourHandY)
00121       : ClockHand(n,x,y) { }
00122    ~HourHand() { }
00123 
00124    Float_t GetHandAngle() { return 30.*(GetHour()%12 + GetMinute()/60.); }
00125 };
00126 
00127 
00128 
00129 class SecondHand : public ClockHand {
00130 
00131 private:
00132    static Float_t fgSecondHandX[];
00133    static Float_t fgSecondHandY[];
00134 
00135 protected:
00136    UInt_t GetTimeValue() { return GetSecond(); }   // used to update every second
00137 
00138 public:
00139    SecondHand(Int_t n=4, Float_t *x=fgSecondHandX, Float_t *y=fgSecondHandY)
00140       : ClockHand(n,x,y) { }
00141    ~SecondHand() { }
00142 
00143    Float_t GetHandAngle() { return  6.*GetSecond(); }
00144 };
00145 
00146 
00147 
00148 class Aclock : public TTimer {
00149 
00150 private:
00151    TPad       *fPad;            // pad where this clock is drawn
00152    MinuteHand *fMinuteHand;     // minute hand
00153    HourHand   *fHourHand;       // hour hand
00154    SecondHand *fSecondHand;     // second hand
00155 
00156 public:
00157    Aclock(Int_t csize=100);
00158    virtual ~Aclock();
00159 
00160    virtual Bool_t Notify();
00161    void   Paint(Option_t *option);
00162    void   Animate();
00163 
00164    ClassDef(Aclock,0)  // analog clock = xclock
00165 };
00166 
00167 #endif   // ACLOCK

Generated on Tue Jul 5 15:14:46 2011 for ROOT_528-00b_version by  doxygen 1.5.1