Difference between revisions of "Drawing with canvas"

From Lazarus wiki
Jump to navigationJump to search
m (Correct canvas image)
 
(5 intermediate revisions by 3 users not shown)
Line 2: Line 2:
  
 
'''Drawing with canvas''' can be done using several procedures e.g.
 
'''Drawing with canvas''' can be done using several procedures e.g.
* [[Canvas line]], draws a line from coordinates (x1, y1) to (x2, y2)
+
* [[Canvas line]], draws a line from coordinates (x1,y1) to (x2,y2)
* [[Canvas rectangle]], draws a rectangle from upper left (x1, y1) to lower right (x2, y2)
+
* [[Canvas rectangle]], draws a rectangle from upper left (x1,y1) to lower right (x2,y2)
* [[Canvas ellipse]], draws an ellipse in the rectangle defined by the focal points (x1, y1) and (x2, y2)
+
* [[Canvas ellipse]], draws an ellipse in a rectangle defined by (x1,y1) and (x2,y2). If x2-x1 = y2-y1, the ellipse will be a circle with radius (x2-x1)/2.
  
The following code draws the diagonals. Enter the code between the begin and the end of the procedure Button1Click:
+
In the Canvas object the <code>Brush</code> and <code>Pen</code> objects are defined, both with a <code>Color</code> property, which indicate the color that makes the fill and stroke of the various objects that are drawn. To paint an object of one color, the first thing is to change the brush and color, before giving the instruction to draw, the order of the statements is important. This would be the code, notice how the color is changed before drawing the ellipse:
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
procedure TForm1.Button1Click(Sender: TObject);
+
  Canvas.Brush.Color:= clRed;
begin
+
  Canvas.Ellipse(195, 117, 205, 128);
   canvas.Line(0,0, form1.Width,form1.Height);
+
  Canvas.Brush.Color:= clBlue;
   canvas.Line(0,form1.height,form1.width,0);
+
  Canvas.Rectangle (192, 130,208,160);
end;
+
  Canvas.Brush.Color:= clGreen;
 +
   Canvas.Rectangle (187, 130,191,162);
 +
  Canvas.Brush.Color:= clYellow;
 +
  Canvas.Rectangle (209, 130,213,162);
 +
  Canvas.Brush.Color:= clMaroon;
 +
   Canvas.Rectangle (193,161,199,200);
 +
  Canvas.Brush.Color:= clPurple;
 +
  Canvas.Rectangle (201,161,207,200);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
In the Canvas object are objects called '''Brush''' and  '''Pen''', both with a color property, which indicate the color that makes the fill and stroke of the various objects that are drawn.
+
or shorter:
To paint an object of one color, the first thing is to change the brush and color, before giving the instruction to draw, the order of the statements is important. This would be the code, notice how the color is changed first and then given the instruction to draw:
 
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
  canvas.Brush.color:= clred;
+
   with Canvas do begin
  canvas.Ellipse(195, 117, 205, 128);
+
     Brush.Color:= clRed;
  canvas.Brush.color:= clblue;
 
  canvas.Rectangle (192, 130,208,160);
 
  canvas.Brush.color:= clgreen;
 
  canvas.Rectangle (187, 130,191,162);
 
  canvas.Brush.color:= clyellow;
 
  canvas.Rectangle (209, 130,213,162);
 
  canvas.Brush.color:= clmaroon;
 
  canvas.Rectangle (193,161,199,200);
 
  canvas.Brush.color:= clpurple;
 
  canvas.Rectangle (201,161,207,200);
 
</syntaxhighlight>
 
 
 
or shorter
 
<syntaxhighlight>
 
   with canvas do begin
 
     Brush.color:= clred;
 
 
     Ellipse(195, 117, 205, 128);
 
     Ellipse(195, 117, 205, 128);
     Brush.color:= clblue;
+
     Brush.Color:= clBlue;
 
     Rectangle (192, 130,208,160);
 
     Rectangle (192, 130,208,160);
     Brush.color:= clgreen;
+
     Brush.Color:= clGreen;
 
     Rectangle (187, 130,191,162);
 
     Rectangle (187, 130,191,162);
     Brush.color:= clyellow;
+
     Brush.Color:= clYellow;
 
     Rectangle (209, 130,213,162);
 
     Rectangle (209, 130,213,162);
     Brush.color:= clmaroon;
+
     Brush.Color:= clMaroon;
 
     Rectangle (193,161,199,200);
 
     Rectangle (193,161,199,200);
     Brush.color:= clpurple;
+
     Brush.Color:= clPurple;
 
     Rectangle (201,161,207,200);
 
     Rectangle (201,161,207,200);
 
   end;
 
   end;
Line 53: Line 43:
  
 
This will give something like this:  
 
This will give something like this:  
[[Image:canvas3.png]]
+
[[Image:canvas.png]]

Latest revision as of 23:35, 27 February 2021

Deutsch (de) English (en) français (fr) 中文(中国大陆)‎ (zh_CN)

Drawing with canvas can be done using several procedures e.g.

  • Canvas line, draws a line from coordinates (x1,y1) to (x2,y2)
  • Canvas rectangle, draws a rectangle from upper left (x1,y1) to lower right (x2,y2)
  • Canvas ellipse, draws an ellipse in a rectangle defined by (x1,y1) and (x2,y2). If x2-x1 = y2-y1, the ellipse will be a circle with radius (x2-x1)/2.

In the Canvas object the Brush and Pen objects are defined, both with a Color property, which indicate the color that makes the fill and stroke of the various objects that are drawn. To paint an object of one color, the first thing is to change the brush and color, before giving the instruction to draw, the order of the statements is important. This would be the code, notice how the color is changed before drawing the ellipse:

  Canvas.Brush.Color:= clRed;
  Canvas.Ellipse(195, 117, 205, 128);
  Canvas.Brush.Color:= clBlue;
  Canvas.Rectangle (192, 130,208,160);
  Canvas.Brush.Color:= clGreen;
  Canvas.Rectangle (187, 130,191,162);
  Canvas.Brush.Color:= clYellow;
  Canvas.Rectangle (209, 130,213,162);
  Canvas.Brush.Color:= clMaroon;
  Canvas.Rectangle (193,161,199,200);
  Canvas.Brush.Color:= clPurple;
  Canvas.Rectangle (201,161,207,200);

or shorter:

  with Canvas do begin
    Brush.Color:= clRed;
    Ellipse(195, 117, 205, 128);
    Brush.Color:= clBlue;
    Rectangle (192, 130,208,160);
    Brush.Color:= clGreen;
    Rectangle (187, 130,191,162);
    Brush.Color:= clYellow;
    Rectangle (209, 130,213,162);
    Brush.Color:= clMaroon;
    Rectangle (193,161,199,200);
    Brush.Color:= clPurple;
    Rectangle (201,161,207,200);
  end;

This will give something like this: canvas.png