Difference between revisions of "Drawing with canvas"

From Lazarus wiki
(article is not part of Tao Yue's Tuturial)
Line 1: Line 1:
 
{{Drawing with canvas}}
 
{{Drawing with canvas}}
  
published by leo_tecnologica@yahoo.com.ar --- Leonardo Gabriel Calautti
+
'''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 the rectangle defined by the focal points (x1, y1) and  (x2, y2)
  
'''Line''' (x1, y1, x2, y2)-> draw a line from point (x1, y1) to (x2, y2)
+
The following code draws the diagonals. Enter the code between the begin and the end of the procedure Button1Click:
 
 
'''Rectangle''' (x1, y1, x2, y2)-> draw a rectangle with a vertex at the point (x1, y1) and the opposite at the point (x2, y2)
 
 
 
'''Ellipse''' (x1, y1, x2, y2)-> draws an ellipse in the rectangle defined by the point (x1, y1) and point (x2, y2)
 
 
 
[[Image:canvas2.png]]
 
 
 
For example, the following code draws the diagonals. Enter the code between the begin and the end of the function Button1Click:
 
  
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 21: Line 16:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
How can you paint the inside of the rectangles and ellipses?
+
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.
There in the Canvas object, an object called '''Brush''' and a '''Pen''', 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 first and then given the instruction to draw:
To paint an object of one color, the first thing is to change the brush and brush, before giving the instruction to draw ... the order is important. This would be our code, notice how the color is changed first and then given the instruction to draw:
 
  
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 40: Line 34:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
If you follow the steps, you may have done something like this:[[Image:canvas3.png]]
+
or shorter
 +
<syntaxhighlight>
 +
  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;
 +
</syntaxhighlight>
 +
 
 +
This will give something like this:  
 +
[[Image:canvas3.png]]

Revision as of 23:02, 5 November 2016

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 the rectangle defined by the focal points (x1, y1) and (x2, y2)

The following code draws the diagonals. Enter the code between the begin and the end of the procedure Button1Click:

procedure TForm1.Button1Click(Sender: TObject);
begin
  canvas.Line(0,0, form1.Width,form1.Height);
  canvas.Line(0,form1.height,form1.width,0);
end;

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. 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:

  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: canvas3.png