Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
canvas.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphics
3/// \notebook -js
4/// \preview 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{
33 TCanvas *c1 = new TCanvas("c1", "Canvas Example", 200, 10, 600, 480);
34
35 gBenchmark->Start("canvas");
36
37 // Inside this canvas, we create two pads
38 TPad *pad1 = new TPad("pad1", "This is pad1", 0.05, 0.52, 0.95, 0.97);
39 TPad *pad2 = new TPad("pad2", "This is pad2", 0.05, 0.02, 0.95, 0.47);
40 pad1->SetFillColor(11);
41 pad2->SetFillColor(11);
42 pad1->Draw();
43 pad2->Draw();
44
45 // A pad may contain other pads and graphics objects.
46 // We set the current pad to pad2.
47 // Note that the current pad is always highlighted.
48 pad2->cd();
49 TPad *pad21 = new TPad("pad21", "First subpad of pad2", 0.02, 0.05, 0.48, 0.95, 17, 3);
50 TPad *pad22 = new TPad("pad22", "Second subpad of pad2", 0.52, 0.05, 0.98, 0.95, 17, 3);
51 pad21->Draw();
52 pad22->Draw();
53
54 // We enter some primitives in the created pads and set some attributes
55 pad1->cd();
56 float xt1 = 0.5;
57 float yt1 = 0.1;
58 TText *t1 = new TText(0.5, yt1, "ROOT");
59 t1->SetTextAlign(22);
60 t1->SetTextSize(0.05);
61 t1->Draw();
62 TLine *line1 = new TLine(0.05, 0.05, 0.80, 0.70);
63 line1->SetLineWidth(8);
64 line1->SetLineColor(2);
65 line1->Draw();
66 line1->DrawLine(0.6, 0.1, 0.9, 0.9);
67 TLine *line2 = new TLine(0.05, 0.70, 0.50, 0.10);
68 line2->SetLineWidth(4);
69 line2->SetLineColor(5);
70 line2->Draw();
71
72 pad21->cd();
73 TText *t21 = new TText(0.05, 0.8, "This is pad21");
74 t21->SetTextSize(0.1);
75 t21->Draw();
76 float xp2 = 0.5;
77 float yp2 = 0.4;
78 TPavesText *paves = new TPavesText(0.1, 0.1, xp2, yp2);
79 paves->AddText("This is a PavesText");
80 paves->AddText("You can add new lines");
81 paves->AddText("Text formatting is automatic");
82 paves->SetFillColor(43);
83 paves->Draw();
84 pad22->cd();
85 TText *t22 = new TText(0.05, 0.8, "This is pad22");
86 t22->SetTextSize(0.1);
87 t22->Draw();
88 float xlc = 0.01;
89 float ylc = 0.01;
90 TPaveLabel *label = new TPaveLabel(xlc, ylc, xlc + 0.8, ylc + 0.1, "This is a PaveLabel");
91 label->SetFillColor(24);
92 label->Draw();
93
94 // Modify object attributes in a loop
95 Int_t nloops = 50;
96 float dxp2 = (0.9 - xp2) / nloops;
97 float dyp2 = (0.7 - yp2) / nloops;
98 float dxlc = (0.1 - xlc) / nloops;
99 float dylc = (0.4 - xlc) / nloops;
100 float dxt1 = (0.5 - xt1) / nloops;
101 float dyt1 = (0.8 - yt1) / nloops;
102 float t10 = t1->GetTextSize();
103 float t1end = 0.3;
104 float t1ds = (t1end - t10) / nloops;
105 Int_t color = 0;
106 for (int i = 0; i < nloops; i++) {
107 color++;
108 color %= 8;
109 line1->SetLineColor(color);
110 t1->SetTextSize(t10 + t1ds * i);
111 t1->SetTextColor(color);
112 t1->SetX(xt1 + dxt1 * i);
113 t1->SetY(yt1 + dyt1 * i);
114 pad1->Modified();
115 paves->SetX2NDC(xp2 + dxp2 * i);
116 paves->SetY2NDC(yp2 + dyp2 * i);
117 pad21->Modified();
118 label->SetX1NDC(xlc + dxlc * i);
119 label->SetY1NDC(ylc + dylc * i);
120 label->SetX2NDC(xlc + dxlc * i + 0.8);
121 label->SetY2NDC(ylc + dylc * i + 0.2);
122 pad22->Modified();
123 c1->Update();
124 }
125 gBenchmark->Show("canvas");
126}
int Int_t
Definition RtypesCore.h:45
R__EXTERN TBenchmark * gBenchmark
Definition TBenchmark.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:38
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:45
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:42
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:103
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:293
The most important graphics class in the ROOT system.
Definition TPad.h:28
A Pave (see TPave) with a text centered in the Pave.
Definition TPaveLabel.h:20
void Draw(Option_t *option="") override
Draw this pavelabel with its current attributes.
virtual void SetY1NDC(Double_t y1)
Definition TPave.h:86
virtual void SetY2NDC(Double_t y2)
Definition TPave.h:87
virtual void SetX1NDC(Double_t x1)
Definition TPave.h:84
virtual void SetX2NDC(Double_t x2)
Definition TPave.h:85
A PaveText (see TPaveText) with several stacked paves.
Definition TPavesText.h:18
Base class for several text objects.
Definition TText.h:22
return c1
Definition legend1.C:41
auto * t1
Definition textangle.C:20