Drawing a simple Line lazarus / 39-Graphics ---------------------------------------------------------------------- seany · 2016-04-18 11:35 Hello. Consider this tform: 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 TForm1 = class(TForm)    execBtn: TButton;    inputTxt1: TMemo;    PaintBox1: TPaintBox;    repeatBtn: TButton;    abortBtn: TButton;    inputTxt: TMemo;    statusLbl: TLabel;    Panel1: TPanel;    procedure execBtnClick(Sender: TObject);    procedure PaintBox1Paint(Sender: TObject);    procedure Panel1Click(Sender: TObject);    procedure Panel1Paint(Sender: TObject);    procedure parse(instruction:string);    procedure tokenize (const Delimiter: Char; Input: string; const Strings: TStrings);  private    { private declarations }  public    { public declarations }    var       viewMode : string;  end;                       and this snippet : 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.PaintBox1Paint(Sender: TObject);begin           case viewMode of          'projectManagement' : begin                              ShowMessage('1');               PaintBox1.canvas.Brush.Color:=clblue;               ShowMessage('2');               PaintBox1.canvas.Pen.Width:=4;               PaintBox1.Canvas.Pen.Color := clBlue;               PaintBox1.Canvas.Pen.Style := psSolid;               ShowMessage('3');               PaintBox1.Canvas.MoveTo(10,10);             ShowMessage('4');               PaintBox1.Canvas.LineTo(50,50);               ShowMessage('5');                              end;     end;end;    1 The nippet is trying to draw a line from 10, 10 to 50,50. It is coming until the showmessage('4') without errors. Then the lineto (I also tested with line() ) is throwing the external SIGSEGV error. Machine is 64 bit linux (Arch). Lazarus is 1.6 Any help? ---------------------------------------------------------------------- Graeme · 2016-04-18 11:55 It works fine here under 64-bit FreeBSD with LCL-GTK2. I simply removed your ShowMessage() calls. Hint #1:   Don't use ShowMessage() to debug paint events. Use writeln() instead and make sure you have a console window available to see the output. Coincidently, the same code using TAgg2D (AggPas). 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 TMainForm.DoAggPainting;begin  // Paint composedimage white  FAgg2D.ClearAll(255, 255, 255);  FAgg2D.LineWidth(4);  FAgg2D.LineColor($00, $00, $FF);  FAgg2D.Line(10, 10, 50, 50);end;  Hint #2:   AggPas will give you much superior painting results than the crappy TPaintBox or LCL TCanvas classes. Using the TAgg2D class hides all the complexity (more advanced features) of using AGG too. ---------------------------------------------------------------------- Handoko · 2016-04-18 12:15 @seany: Have you tried to move the things inside TForm1.PaintBox1Paint to another event? Perhaps move them to TForm1.Button1Click. It works on my computer if I do so. ---------------------------------------------------------------------- Graeme · 2016-04-18 12:23 @Handoko: You are only supposed to paint inside OnPaint events, thus if seany's TForm1.PaintBox1Paint() is the event handler for PaintBox1.OnPaint then there is nothing wrong with that painting code. On the other had, AggPas allows you to paint at any time - you are not limited to OnPaint event handlers only. That's because AggPas paints to its own memory pixel buffer, which you then later bitblit to whatever Canvas (the latter part only, is done in a OnPaint event handler). ---------------------------------------------------------------------- Handoko · 2016-04-18 12:45 I know, actually I don't suggest him to put the painting commands outside the OnPaint event. I want to let him know it is bad to put ShowMessage inside OnPaint. I don't tell him directly because I want him to figure out by himself. If he follows what I said, he later may ask why it will work if putting them outside. So I will explain to him later. It will be better if one makes mistakes and learns from the experience rather than telling him directly what one should not do. That's the best way to teach beginners, I'm a freelance computer teacher. ---------------------------------------------------------------------- seany · 2016-04-18 15:38 @all Ofcourse I tried it with a buttonclick. Incidentally, that doesn't throw an exception, but that doesn'T draw anything either. Researching a little bit told me that i need to draw within the paint event. My initial attempts were without showmessages. That also threw the error. I pinpointed the error line with the messages. ---------------------------------------------------------------------- Handoko · 2016-04-18 19:53 In my case, if I move the drawing commands into a button click event, it works without throwing any error. You can test my code that I provided in the attachment. If mine works correctly but yours don't, that means there is something wrong in your code. Or perhaps that's how Windows works. I only tested the generated exe file of my code on Wine (I'm a Linux user), it works correctly here. Just like Graeme said, you really should not use ShowMessage inside painting event. Here is the explanation. You have to understand when OnPaint being called. It is managed automatically by the LCL widget you use and the your operating system (Windows, Linux, ...). Different widget and operating system might behave slightly differently. In my code, this is how the events work: Button clicked > ShowMessage1 > do painting > ShowMessage2 > do painting > ShowMessage3 > do painting > ShowMessage4 > do painting > ShowMessage5 The sequence above works without problem. But ... Now back to your code, this is how the events work: App started > form being painted > PaintBox is now painting > OnPaint is called ... but while it hasn't finished ... it call > ShowMessage ... ShowMessage can't paint correctly because its parent (the form) hasn't finished the painting (Paintbox is a child component of the form) ... bla bla bla So now you can see the problem. They're waiting each other. Depend on your system, on my Linux I don't see any error. What I see is they're waiting each other endlessly. So, you may think the solution is to put the drawing commands outside OnPaint event. No, it is very wrong. You may do it, but you might run into unexpected issue (in the future). You should put your drawing commands inside OnPaint. And remember, it is good to keep it short, no heavy calculations, no performing other heavy tasks, no database accessing, no network processing, also no ShowMessage or other kind of dialogs. Hope you can understand my explanation. Note: The orders of how the events work are very complex, they don't work exactly as I said. I simplify it to make it easy to understand.