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");
}
int Int_t
Definition: RtypesCore.h:41
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:46
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:174
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:157
The Canvas class.
Definition: TCanvas.h:31
A simple line.
Definition: TLine.h:23
virtual TLine * DrawLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Draw this line with new coordinates.
Definition: TLine.cxx:88
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:195
The most important graphics class in the ROOT system.
Definition: TPad.h:29
void Modified(Bool_t flag=1)
Definition: TPad.h:417
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1282
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:591
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.
Definition: TPaveLabel.cxx:77
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:182
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.
Definition: TPavesText.cxx:79
Base class for several text objects.
Definition: TText.h:23
return c1
Definition: legend1.C:41
auto * t1
Definition: textangle.C:20
Author
Rene Brun

Definition in file canvas.C.