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