ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
canvas.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_graphics
3 /// Example of primitives in a canvas.
4 /// One of the first actions in a ROOT session is the creation of a Canvas.
5 /// Here we create a Canvas named "c1".
6 ///
7 /// After having executed this macro, try now to point on any object on the
8 /// screen: pad, text, lines, etc.
9 ///
10 /// When the cursor points to sensitive areas in an object, the cursor
11 /// shape changes and suggests the type of action that can be applied.
12 ///
13 /// For example:
14 /// - One can move, grow,shrink a pad.
15 /// - A text can be moved.
16 /// - A line can be moved or its end points can be modified.
17 /// - One can move, grow and shrink PaveLabels and PavesText.
18 ///
19 /// Point to an object and click the right mouse button to change attributes.
20 /// Try to change the canvas size.
21 ///
22 /// In the canvas "File" menu, select the option "Print" to produce
23 /// a PostScript file with a copy of the canvas.
24 ///
25 /// \macro_image
26 /// \macro_code
27 ///
28 /// \author Rene Brun
29 
30 void canvas(){
31  TCanvas *c1 = new TCanvas("c1","Canvas Example",200,10,600,480);
32 
33  gBenchmark->Start("canvas");
34 
35  // Inside this canvas, we create two pads
36  TPad *pad1 = new TPad("pad1","This is pad1",0.05,0.52,0.95,0.97);
37  TPad *pad2 = new TPad("pad2","This is pad2",0.05,0.02,0.95,0.47);
38  pad1->SetFillColor(11);
39  pad2->SetFillColor(11);
40  pad1->Draw();
41  pad2->Draw();
42 
43  // A pad may contain other pads and graphics objects.
44  // We set the current pad to pad2.
45  // Note that the current pad is always highlighted.
46  pad2->cd();
47  TPad *pad21 = new TPad("pad21","First subpad of pad2",0.02,0.05,0.48,0.95,17,3);
48  TPad *pad22 = new TPad("pad22","Second subpad of pad2",0.52,0.05,0.98,0.95,17,3);
49  pad21->Draw();
50  pad22->Draw();
51 
52  // We enter some primitives in the created pads and set some attributes
53  pad1->cd();
54  float xt1 = 0.5;
55  float yt1 = 0.1;
56  TText *t1 = new TText(0.5,yt1,"ROOT");
57  t1->SetTextAlign(22);
58  t1->SetTextSize(0.05);
59  t1->Draw();
60  TLine *line1 = new TLine(0.05,0.05,0.80,0.70);
61  line1->SetLineWidth(8);
62  line1->SetLineColor(2);
63  line1->Draw();
64  line1->DrawLine(0.6,0.1,0.9,0.9);
65  TLine *line2 = new TLine(0.05,0.70,0.50,0.10);
66  line2->SetLineWidth(4);
67  line2->SetLineColor(5);
68  line2->Draw();
69 
70  pad21->cd();
71  TText *t21 = new TText(0.05,0.8,"This is pad21");
72  t21->SetTextSize(0.1);
73  t21->Draw();
74  float xp2 = 0.5;
75  float yp2 = 0.4;
76  TPavesText *paves = new TPavesText(0.1,0.1,xp2,yp2);
77  paves->AddText("This is a PavesText");
78  paves->AddText("You can add new lines");
79  paves->AddText("Text formatting is automatic");
80  paves->SetFillColor(43);
81  paves->Draw();
82  pad22->cd();
83  TText *t22 = new TText(0.05,0.8,"This is pad22");
84  t22->SetTextSize(0.1);
85  t22->Draw();
86  float xlc = 0.01;
87  float ylc = 0.01;
88  TPaveLabel *label = new TPaveLabel(xlc, ylc, xlc+0.8, ylc+0.1,"This is a PaveLabel");
89  label->SetFillColor(24);
90  label->Draw();
91 
92  // Modify object attributes in a loop
93  Int_t nloops = 50;
94  float dxp2 = (0.9-xp2)/nloops;
95  float dyp2 = (0.7-yp2)/nloops;
96  float dxlc = (0.1-xlc)/nloops;
97  float dylc = (0.4-xlc)/nloops;
98  float dxt1 = (0.5-xt1)/nloops;
99  float dyt1 = (0.8-yt1)/nloops;
100  float t10 = t1->GetTextSize();
101  float t1end = 0.3;
102  float t1ds = (t1end - t10)/nloops;
103  Int_t color = 0;
104  for (int i=0;i<nloops;i++) {
105  color++;
106  color %= 8;
107  line1->SetLineColor(color);
108  t1->SetTextSize(t10 + t1ds*i);
109  t1->SetTextColor(color);
110  t1->SetX(xt1+dxt1*i);
111  t1->SetY(yt1+dyt1*i);
112  pad1->Modified();
113  paves->SetX2NDC(xp2+dxp2*i);
114  paves->SetY2NDC(yp2+dyp2*i);
115  pad21->Modified();
116  label->SetX1NDC(xlc+dxlc*i);
117  label->SetY1NDC(ylc+dylc*i);
118  label->SetX2NDC(xlc+dxlc*i+0.8);
119  label->SetY2NDC(ylc+dylc*i+0.2);
120  pad22->Modified();
121  c1->Update();
122  }
123  gBenchmark->Show("canvas");
124 }
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
virtual void SetX2NDC(Double_t x2)
Definition: TPave.h:88
virtual void SetX(Double_t x)
Definition: TText.h:91
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
Definition: TPaveText.cxx:160
virtual TLine * DrawLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Draw this line with new coordinates.
Definition: TLine.cxx:93
virtual Float_t GetTextSize() const
Definition: TAttText.h:50
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
int Int_t
Definition: RtypesCore.h:41
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:254
virtual void Draw(Option_t *option="")
Draw this pavestext with its current attributes.
Definition: TPavesText.cxx:79
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:514
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
Base class for several text objects.
Definition: TText.h:42
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1192
virtual void SetTextAlign(Short_t align=11)
Definition: TAttText.h:55
A Pave (see TPave) with a text centered in the Pave.
Definition: TPaveLabel.h:32
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
virtual void SetX1NDC(Double_t x1)
Definition: TPave.h:87
The most important graphics class in the ROOT system.
Definition: TPad.h:46
A simple line.
Definition: TLine.h:41
virtual void SetY2NDC(Double_t y2)
Definition: TPave.h:90
The Canvas class.
Definition: TCanvas.h:48
virtual void Draw(Option_t *option="")
Draw this pavelabel with its current attributes.
Definition: TPaveLabel.cxx:77
virtual void SetTextColor(Color_t tcolor=1)
Definition: TAttText.h:57
virtual void SetY(Double_t y)
Definition: TText.h:92
TText * t10
Definition: rootenv.C:95
virtual void SetTextSize(Float_t tsize=1)
Definition: TAttText.h:60
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
A PaveText (see TPaveText) with several stacked paves.
Definition: TPavesText.h:28
void Modified(Bool_t flag=1)
Definition: TPad.h:407
virtual void SetY1NDC(Double_t y1)
Definition: TPave.h:89