Drawing lines and primitives lazarus / 13-General ---------------------------------------------------------------------- FreeMind · 2009-03-30 16:51 Here goes my newbyness again: I can't seem to find this on the net. I create an application with a form. Where could i find the functions to draw simple coloured lines on forms? ---------------------------------------------------------------------- Ñuño_Martínez · 2009-03-30 17:54 You can't draw directly on a form. (Ok, actually you can, but that's not a good idea, trust me). To draw on a form first add a TPaintBox (at "Additonal" tab), align it to client area. Now use the search to find how to draw on a TPaintBox. ---------------------------------------------------------------------- FreeMind · 2009-03-31 17:17 Thank you very much. And now... er... When i draw my lines in the "paint" event, they are refreshed only when i rescale the window. Is there any way to redraw on screen refresh? ---------------------------------------------------------------------- Ñuño_Martínez · 2009-03-31 19:08 I think you should read this document. It will learn you how to use graphics with Lazarus. ---------------------------------------------------------------------- FreeMind · 2009-03-31 20:41 Still. I didn't find what i needed in that page... Thanks anyway. The problem remains: I am drawing a line in the paint event. The line position is random (just to test the refreshing). So, when the program runs, that lines stands still, which means, that the paint event happens only once. Only when i rescale the window, the line starts jumping into random positions. Is there any way i can make the line refresh whenever i tell it to, for ehample, every two screen refreshes or somethin? I can't see any loops in this code.... ---------------------------------------------------------------------- Ñuño_Martínez · 2009-03-31 23:13 Quote from: FreeMind on March 31, 2009, 10:41:08 pmIs there any way i can make the line refresh whenever i tell it to, for ehample, every two screen refreshes or somethin? I can't see any loops in this code.... So you want to do an animation. That's different. And there are different ways depending on why do you do want to show an animation. If you want to show an animation on a window you can use a TTimer object (in the "System" tab). Set the Interval in milliseconds and set it's OnTimer event to tell the windows manager you want to redraw the window and all it's content this way: Code: [Select]procedure TForm1.Timer1Timer(Sender: TObject); begin { "SELF" means "the current object". }   SELF.Invalidate; end; Actually, there's a loop inside the Run method of the Application object. It's called "the message loop" because it catches messages from the window manager and sends them to the appropriate objects. That loop is different on different platforms (Win32, X11, Cocoa, etc.). If you're wondering how to do an "old style" animation then you shouldn't use the LCL. You should create a console application and use a different graphic library such as the one included with the Free Pascal compiler (the Graph unit) or install another one, as Allegro.pas. ---------------------------------------------------------------------- FreeMind · 2009-04-01 11:29 Whoah, you are the best. Anyway, this is redrawing the whole form, right? ---------------------------------------------------------------------- Martin_fr · 2009-04-01 12:00 If you want to force a component (or form) to redraw, you can use the "Invalidate" method. You can call it from within a timer. Question is why do you want to Paint inside the OnPaint event? If you use a TPaintBox, you can just paint on it, it will refresh on it's own, and your painted data persists. The OnPaintEvent you only need if you want to interfere with the way a component draws itself. Or if you have a component that doesn't draw itself, and expects you to do so in the Paint event. ---------------------------------------------------------------------- FreeMind · 2009-04-01 12:18 Question is why do you want to Paint inside the OnPaint event? If you use a TPaintBox, you can just paint on it, it will refresh on it's own, and your painted data persists. How do you suggest painting on a Tpaintbox without the paint event? A timer? Ok, so ive done it like this: Code: [Select]procedure TForm1.RedrawTimer(Sender: TObject); begin    TheImage := TBitmap.Create;   TheImage.Height := UVpaint.Height;   TheImage.Width := UVpaint.Width;   TheImage.Canvas.Brush.Color :=$000000;   TheImage.canvas.FillRect( Bounds(0,0,UVpaint.width,UVpaint.height));   TheImage.Canvas.Pen.Color :=$FFFFFF;   TheImage.canvas.MoveTo(100, 100);   TheImage.canvas.LineTo(200, 200+random(20));   TheImage.canvas.LineTo(100, 200+random(20));   TheImage.canvas.LineTo(100, 100);   TheImage.Canvas.Brush.Color :=clblue;   TheImage.Canvas.FloodFill(110, 120, clwhite, fsBorder);   UVpaint.Canvas.Draw(0, 0, TheImage);   TheImage.free; end;  It draws and refreshes good. Though it does flicker when i rescale the window. Any tips? Since the draw event only happens on open and on rescale, i thought i should draw the same bitmap on the draw event... But in that case i have no place to put the "TheImage.free" ---------------------------------------------------------------------- arnoldb · 2009-04-01 13:57 You can paint on a TPaintbox anywhere from within your program where the paintbox is in scope. Try the following - this will draw random lines or circles, depending on which button you click: Create a new project.  On Form1, put a TPaintbox, a TColorListBox, a Button1 and a Button2. Change caption for Button1 to 'Random Line', and caption for Button2 to 'Random Circle'. Assign the following code to the OnClick events of the buttons: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); var x1,y1,x2,y2,c:integer;     aColor:TColor; begin   x1:=random(PaintBox1.Width);   x2:=random(PaintBox1.Width);   y1:=random(PaintBox1.Height);   y2:=random(PaintBox1.Height);   c:=random(colorlistbox1.Items.Count);   aColor:=ColorListBox1.Colors[c];   With Paintbox1 do   begin     canvas.pen.Color :=aColor;     canvas.Line(x1,y1,x2,y2);   end; end; procedure TForm1.Button2Click(Sender: TObject); var x,y,r,c:integer;     aColor:TColor; begin   x:=random(PaintBox1.Width);   y:=random(PaintBox1.Height);   r:=random(PaintBox1.Height div 3);   c:=random(colorlistbox1.Items.Count);   aColor:=ColorListBox1.Colors[c];   With Paintbox1 do   begin     canvas.pen.Color :=aColor;     canvas.Brush.Color := aColor;     canvas.EllipseC(x,y,r,r) ;   end; end;                Compile & Run.  Each time you click a button, something will happen on the paintbox.  I was just lazy and used the TColorListBox to get a random range of colours. You can even add a timer, and in it's OnTimer event add the same. Not a very practical example, but it might help you. ---------------------------------------------------------------------- FreeMind · 2009-04-01 15:14 I guess i found a perfect way of doing it: Code: [Select]procedure TForm1.FormCreate(Sender: TObject); begin    TheImage := TBitmap.Create; end; procedure TForm1.RedrawTimer(Sender: TObject); begin   TheImage.free;   TheImage := TBitmap.Create;   TheImage.Height := UVpaint.Height;   TheImage.Width := UVpaint.Width;   TheImage.Canvas.Brush.Color :=$000000;   TheImage.canvas.FillRect( Bounds(0,0,UVpaint.width,UVpaint.height));   TheImage.Canvas.Pen.Color :=$FFFFFF;   TheImage.canvas.MoveTo(100, 100);   TheImage.canvas.LineTo(200, 200+random(20));   TheImage.canvas.LineTo(100, 200+random(20));   TheImage.canvas.LineTo(100, 100);   TheImage.Canvas.Brush.Color :=clblue;   TheImage.Canvas.FloodFill(110, 120, clwhite, fsBorder);   UVpaint.Canvas.Draw(0, 0, TheImage); end; procedure TForm1.UVpaintPaint(Sender: TObject); begin   UVpaint.Canvas.Draw(0, 0, TheImage); end; ---------------------------------------------------------------------- Ñuño_Martínez · 2009-04-01 15:32 Quote from: FreeMind on April 01, 2009, 01:29:35 pmWhoah, you are the best. I know... ---------------------------------------------------------------------- Handoko · 2018-01-22 11:40 Sorry to dig an old thread out. I'm just curious. Quote from: FreeMind on April 01, 2009, 02:18:15 pmQuestion is why do you want to Paint inside the OnPaint event? If you use a TPaintBox, you can just paint on it, it will refresh on it's own, and your painted data persists. I don't understand why you avoid to paint inside the OnPaint event. I modified your code to paint inside OnPaint, the result ==> much simpler code: 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, Forms, Controls, Graphics, ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    Timer1: TTimer;    uvPaint: TPaintBox;    procedure Timer1Timer(Sender: TObject);    procedure uvPaintPaint(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Timer1Timer(Sender: TObject);begin  uvPaint.Invalidate;end; procedure TForm1.uvPaintPaint(Sender: TObject);begin  with uvPaint.Canvas do begin    Brush.Color := clBlack;    FillRect(Bounds(0, 0, Width, Height));    Pen.Color := clWhite;    MoveTo(100, 100);    LineTo(200, 200 + Random(20));    LineTo(100, 200 + Random(20));    LineTo(100, 100);    Brush.Color := clBlue;    FloodFill(110, 120, clWhite, fsBorder);  end;end; end. Note: - The screenshot captured when running the program on Linux Gtk2 - FloodFill has issue when running on Gtk2 - The animation runs smoothly, no flicker even when resizing