Trying to draw rectangles in random positions lazarus / 39-Graphics ---------------------------------------------------------------------- Prado · 2019-09-08 15:14 I'm trying to generate random points and then drawing rectangles in those positions, after pressing a button. The problem is that the rectangles are drawn only inside the button, instead of the entire window. Code of Unit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  MAX_POINTS = 300; var  Form1: TForm1;  Points : array [0 .. MAX_POINTS - 1] of TPoint;  {isDrawing : boolean;} implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var  i:integer;begin  Randomize;  {isDrawing := False;}  i := 0;  while i < MAX_POINTS do  begin    Points[i] := Point(0, 0);    i := i + 1;  end;end; procedure TForm1.FormPaint(Sender: TObject);var  i:integer;begin    i := 0;    while i < MAX_POINTS do      begin      Canvas.Pen.Color := clBlack;        Canvas.Rectangle(Points[i].x, Points[i].y, Points[i].x + 6, Points[i].y + 6);        i := i + 1;    end;end; procedure TForm1.BRandomClick(Sender: TObject);var  px:integer;  py:integer;  i:integer;begin  i := 0;  while i < MAX_POINTS do  begin    px := random(Canvas.Width);    py := random(Canvas.Height);    Points[i].x := px;    Points[i].y := py;    i := i + 1;  end;end; ---------------------------------------------------------------------- bigeno · 2019-09-08 15:29 Then don't use BRandom.Canvas ---------------------------------------------------------------------- wp · 2019-09-08 15:39 Maybe I did not set up my test program correctly, but for me there is only a single point at the top-left corner of the form (It would be better if you'd post a compilable project instead of a code snippet, this avoids any misunderstandings). I do get a form full of points when I - use the form's width and height instead of the Canvas width/height. In fact, I was not even aware that the canvas has a width and a height, I always thought that it is "infinite" - maybe it is the size of the clip rect? - force repainting of the form by calling Invalidate at the end of the button-click procedure. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  px:integer;  py:integer;  i:integer;begin  i := 0;  while i < MAX_POINTS do  begin    px := random(Width); //random(Canvas.Width);  <-- replaced    py := random(Height); //random(Canvas.Height);    Points[i].x := px;    Points[i].y := py;    i := i + 1;  end;  Invalidate;  // <--- addedend; ---------------------------------------------------------------------- Prado · 2019-09-08 15:44 Quote from: wp on September 08, 2019, 05:39:39 pmMaybe I did not set up my test program correctly, but for me there is only a single point at the top-left corner of the form (It would be better if you'd post a compilable project instead of a code snippet, this avoids any misunderstandings). I do get a form full of points when I - use the form's width and height instead of the Canvas width/height. In fact, I was not even aware that the canvas has a width and a height, I always thought that it is "infinite" - maybe it is the size of the clip rect? - force repainting of the form by calling Invalidate at the end of the button-click procedure. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  px:integer;  py:integer;  i:integer;begin  i := 0;  while i < MAX_POINTS do  begin    px := random(Width); //random(Canvas.Width);  <-- replaced    py := random(Height); //random(Canvas.Height);    Points[i].x := px;    Points[i].y := py;    i := i + 1;  end;  Invalidate;  // <--- addedend; Thanks, it works now. I forgot to mention the rectangle at the corner.