Logo ROOT  
Reference Guide
canvas.C File Reference

Detailed Description

View in nbviewer Open in SWAN Example of primitives in a canvas. One of the first actions in a ROOT session is the creation of a Canvas. Here we create a Canvas named "c1".

After having executed this macro, try now to point on any object on the screen: pad, text, lines, etc.

When the cursor points to sensitive areas in an object, the cursor shape changes and suggests the type of action that can be applied.

For example:

  • One can move, grow,shrink a pad.
  • A text can be moved.
  • A line can be moved or its end points can be modified.
  • One can move, grow and shrink PaveLabels and PavesText.

Point to an object and click the right mouse button to change attributes. Try to change the canvas size.

In the canvas "File" menu, select the option "Print" to produce a PostScript file with a copy of the canvas.

void canvas(){
TCanvas *c1 = new TCanvas("c1","Canvas Example",200,10,600,480);
gBenchmark->Start("canvas");
// Inside this canvas, we create two pads
TPad *pad1 = new TPad("pad1","This is pad1",0.05,0.52,0.95,0.97);
TPad *pad2 = new TPad("pad2","This is pad2",0.05,0.02,0.95,0.47);
pad1->SetFillColor(11);
pad2->SetFillColor(11);
pad1->Draw();
pad2->Draw();
// A pad may contain other pads and graphics objects.
// We set the current pad to pad2.
// Note that the current pad is always highlighted.
pad2->cd();
TPad *pad21 = new TPad("pad21","First subpad of pad2",0.02,0.05,0.48,0.95,17,3);
TPad *pad22 = new TPad("pad22","Second subpad of pad2",0.52,0.05,0.98,0.95,17,3);
pad21->Draw();
pad22->Draw();
// We enter some primitives in the created pads and set some attributes
pad1->cd();
float xt1 = 0.5;
float yt1 = 0.1;
TText *t1 = new TText(0.5,yt1,"ROOT");
t1->SetTextAlign(22);
t1->SetTextSize(0.05);
t1->Draw();
TLine *line1 = new TLine(0.05,0.05,0.80,0.70);
line1->SetLineWidth(8);
line1->SetLineColor(2);
line1->Draw();
line1->DrawLine(0.6,0.1,0.9,0.9);
TLine *line2 = new TLine(0.05,0.70,0.50,0.10);
line2->SetLineWidth(4);
line2->SetLineColor(5);
line2->Draw();
pad21->cd();
TText *t21 = new TText(0.05,0.8,"This is pad21");
t21->SetTextSize(0.1);
t21->Draw();
float xp2 = 0.5;
float yp2 = 0.4;
TPavesText *paves = new TPavesText(0.1,0.1,xp2,yp2);
paves->AddText("This is a PavesText");
paves->AddText("You can add new lines");
paves->AddText("Text formatting is automatic");
paves->SetFillColor(43);
paves->Draw();
pad22->cd();
TText *t22 = new TText(0.05,0.8,"This is pad22");
t22->SetTextSize(0.1);
t22->Draw();
float xlc = 0.01;
float ylc = 0.01;
TPaveLabel *label = new TPaveLabel(xlc, ylc, xlc+0.8, ylc+0.1,"This is a PaveLabel");
label->SetFillColor(24);
label->Draw();
// Modify object attributes in a loop
Int_t nloops = 50;
float dxp2 = (0.9-xp2)/nloops;
float dyp2 = (0.7-yp2)/nloops;
float dxlc = (0.1-xlc)/nloops;
float dylc = (0.4-xlc)/nloops;
float dxt1 = (0.5-xt1)/nloops;
float dyt1 = (0.8-yt1)/nloops;
float t10 = t1->GetTextSize();
float t1end = 0.3;
float t1ds = (t1end - t10)/nloops;
Int_t color = 0;
for (int i=0;i<nloops;i++) {
color++;
color %= 8;
line1->SetLineColor(color);
t1->SetTextSize(t10 + t1ds*i);
t1->SetTextColor(color);
t1->SetX(xt1+dxt1*i);
t1->SetY(yt1+dyt1*i);
pad1->Modified();
paves->SetX2NDC(xp2+dxp2*i);
paves->SetY2NDC(yp2+dyp2*i);
pad21->Modified();
label->SetX1NDC(xlc+dxlc*i);
label->SetY1NDC(ylc+dylc*i);
label->SetX2NDC(xlc+dxlc*i+0.8);
label->SetY2NDC(ylc+dylc*i+0.2);
pad22->Modified();
c1->Update();
}
gBenchmark->Show("canvas");
}
Author
Rene Brun

Definition in file canvas.C.

TPave::SetY1NDC
virtual void SetY1NDC(Double_t y1)
Definition: TPave.h:86
TLine::DrawLine
virtual TLine * DrawLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Draw this line with new coordinates.
Definition: TLine.cxx:89
TLine
Definition: TLine.h:22
TPad
Definition: TPad.h:26
TBenchmark::Start
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
Int_t
int Int_t
Definition: RtypesCore.h:45
TPave::SetX2NDC
virtual void SetX2NDC(Double_t x2)
Definition: TPave.h:85
TPavesText::Draw
virtual void Draw(Option_t *option="")
Draw this pavestext with its current attributes.
Definition: TPavesText.cxx:79
TPaveLabel::Draw
virtual void Draw(Option_t *option="")
Draw this pavelabel with its current attributes.
Definition: TPaveLabel.cxx:77
TPad::Modified
void Modified(Bool_t flag=1)
Definition: TPad.h:414
TPave::SetX1NDC
virtual void SetX1NDC(Double_t x1)
Definition: TPave.h:84
TPavesText
Definition: TPavesText.h:18
gBenchmark
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:59
TPave::SetY2NDC
virtual void SetY2NDC(Double_t y2)
Definition: TPave.h:87
TPaveText::AddText
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:183
TPaveLabel
Definition: TPaveLabel.h:20
TObject::Draw
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:197
TText
Definition: TText.h:22
TCanvas
Definition: TCanvas.h:23
t1
auto * t1
Definition: textangle.C:20
TPad::cd
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:594
TBenchmark::Show
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
c1
return c1
Definition: legend1.C:41
TPad::Draw
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1285