Aclock.cxx

Go to the documentation of this file.
00001 // @(#)root/test:$Id: Aclock.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Valeriy Onuchin & Fons Rademakers   04/10/98
00003 
00004 ///////////////////////////////////////////////////////////////////
00005 //  ROOT implementation of the X11 xclock.
00006 //
00007 //  To run this example do the following:
00008 //  $ root
00009 //  root [0] gSystem.Load("libGpad")
00010 //  root [1] gSystem.Load("Aclock")
00011 //  root [1] Aclock a
00012 //  <enjoy>
00013 //  root [2] .q
00014 //
00015 //  Other ROOT fun examples: Tetris, Hello ...
00016 //
00017 //  Begin_Html
00018 // <img src="http://emcal06.rhic.bnl.gov/~onuchin/root/gif/hello_clock.gif">
00019 //  End_Html
00020 //
00021 ///////////////////////////////////////////////////////////////////
00022 
00023 #include <TROOT.h>
00024 #include <TSystem.h>
00025 #include "Aclock.h"
00026 
00027 
00028 ClassImp(Aclock)
00029 
00030 
00031 Float_t MinuteHand::fgMinuteHandX[] = { -0.05, 0, 0.05 };
00032 Float_t MinuteHand::fgMinuteHandY[] = { -0.04, 0.625, -0.04 };
00033 
00034 Float_t HourHand::fgHourHandX[] = { -0.05, 0, 0.05 };
00035 Float_t HourHand::fgHourHandY[] = { -0.04, 0.4, -0.04 };
00036 
00037 Float_t SecondHand::fgSecondHandX[] = { 0., 0.035, 0., -0.035 };
00038 Float_t SecondHand::fgSecondHandY[] = { 0.78, 0.73, 0.68, 0.73 };
00039 
00040 TDatime *ClockHand::fgTime = new TDatime;
00041 
00042 
00043 TPolygon::TPolygon(Int_t n, Float_t *x, Float_t *y) : TPolyLine(n+1,x,y)
00044 {
00045    // Create filled polygon. Polygon will be added to current pad.
00046 
00047    if (!TVirtualPad::Pad()) { Error("Constructor","Create TPad first!"); return; }
00048 
00049    fPad  = (TPad*)TVirtualPad::Pad();
00050    fX[n] = x[0];             // create an extra point to connect polyline ends
00051    fY[n] = y[0];
00052    Draw();                   // append to fPad
00053 }
00054 
00055 void TPolygon::Paint(Option_t *)
00056 {
00057    // Paint filled polygon.
00058 
00059    TAttLine::Modify();
00060    TAttFill::Modify();
00061    fPad->PaintFillArea(fN-1, fX, fY);
00062    fPad->PaintPolyLine(fN, fX, fY, "");
00063 }
00064 
00065 
00066 
00067 ClockHand::ClockHand(Int_t n, Float_t *x, Float_t *y) : TPolygon(n,x,y)
00068 {
00069    // Create clockhand
00070 
00071    fPrevTimeValue = 0;
00072    fX0 = new Float_t[GetN()];   // initial shape and position
00073    fY0 = new Float_t[GetN()];   // initial shape and position
00074 
00075    for (int i = 0; i < GetN(); i++) {
00076      fX0[i] = fX[i];
00077      fY0[i] = fY[i];
00078    }
00079    SetFillColor(9);            // default fill color
00080 }
00081 
00082 void ClockHand::Update()
00083 {
00084    // Update hand position
00085 
00086    if (!IsModified()) return;
00087 
00088    fPrevTimeValue = GetTimeValue();
00089    Move(GetHandAngle());
00090 }
00091 
00092 void ClockHand::Move(Float_t clock_angle)
00093 {
00094    // Move clockhand.
00095 
00096   Int_t n = GetN();
00097 
00098   // ClockPoints used to rotate,scale and shift initial points
00099   static ClockPoints *points = new ClockPoints();
00100 
00101   Float_t wh = (Float_t)fPad->XtoPixel(fPad->GetX2());
00102   Float_t hh = (Float_t)fPad->YtoPixel(fPad->GetY1());
00103 
00104   for (int i = 0; i < n; i++) {
00105      points->SetXY(fX0[i],fY0[i]);
00106      points->Rotate(clock_angle);
00107 
00108      if (wh < hh) points->Scale(0.5,0.5*wh/hh);    //  scale to have a circular clock
00109      else         points->Scale(0.5*hh/wh,0.5);
00110 
00111      points->Shift(0.5,0.5);                      // move to the center of pad
00112      fX[i] = points->GetX();
00113      fY[i] = points->GetY();
00114    }
00115 }
00116 
00117 
00118 Aclock::Aclock(Int_t csize) : TTimer(500, kTRUE)
00119 {
00120    // Create a clock in a new canvas.
00121 
00122    fPad = new TCanvas("Aclock:canvas","xclock",-csize,csize);
00123 
00124    fPad->SetFillColor(14);     // grey
00125 
00126    fMinuteHand = new MinuteHand();
00127    fSecondHand = new SecondHand();
00128    fHourHand   = new HourHand();
00129 
00130    SetBit(kCanDelete);
00131    Draw();                         // append this Aclock to fPad
00132    Animate();
00133    gSystem->AddTimer(this);        // start timer = start animation
00134 }
00135 
00136 Aclock::~Aclock()
00137 {
00138    // Clean up the clock.
00139 
00140    delete fMinuteHand;
00141    delete fSecondHand;
00142    delete fHourHand;
00143 }
00144 
00145 void Aclock::Paint(Option_t *)
00146 {
00147    // Just draw clock scale (time and minutes ticks)
00148 
00149    static ClockPoints *point1 = new ClockPoints();
00150    static ClockPoints *point2 = new ClockPoints();
00151 
00152    Float_t wh = (Float_t)fPad->XtoPixel(fPad->GetX2());
00153    Float_t hh = (Float_t)fPad->YtoPixel(fPad->GetY1());
00154 
00155    for (int i = 0; i < 60; i++ ) {             // draw minute/hour ticks
00156       point1->SetXY(0.,0.9);
00157 
00158       if (!(i%5)) point2->SetXY(0.,0.8);       // hour  ticks  are longer
00159       else        point2->SetXY(0.,0.87);
00160 
00161       Float_t angle = 6.*i;
00162       point1->Rotate(angle);
00163       point2->Rotate(angle);
00164 
00165       if (wh < hh) {                   // scale in oder to draw circle scale
00166          point1->Scale(0.5,0.5*wh/hh);
00167          point2->Scale(0.5,0.5*wh/hh);
00168       } else {
00169          point1->Scale(0.5*hh/wh,0.5);
00170          point2->Scale(0.5*hh/wh,0.5);
00171       }
00172 
00173       point1->Shift(0.5,0.5);              // move to center of pad
00174       point2->Shift(0.5,0.5);
00175 
00176       fPad->PaintLine(point1->GetX(),point1->GetY(),point2->GetX(),point2->GetY());
00177    }
00178 }
00179 
00180 void Aclock::Animate()
00181 {
00182    // Update clock hand positions and redraw the fPad
00183 
00184    if (!fSecondHand->IsModified()) return;
00185 
00186    fHourHand->Update();            //  update position every minute
00187    fMinuteHand->Update();          //  update position every minute
00188    fSecondHand->Update();          //  update position every second
00189    fPad->Modified();               //  drawing ...
00190    fPad->Update();
00191 }
00192 
00193 Bool_t Aclock::Notify()
00194 {
00195    // Actions after timer's time-out
00196 
00197    Animate();
00198    TTimer::Reset();
00199    return kFALSE;
00200 }

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