Recent

Author Topic: Simple line drawing program  (Read 573 times)

Neville

  • Jr. Member
  • **
  • Posts: 52
Simple line drawing program
« on: September 22, 2025, 09:44:21 pm »
I'm trying to get a program to produce some line drawings.  I've chosen to use TImage.  I've followed some information on the Web, but just can't get the first trial line to appear.  Hopefully, I'm doing something very stupid which will be easily rectified. The code is shown below.  Can someone provide me with the missing link to get this working.

With many thanks,   Neville

========================================================================
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
      Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;

type

      { TForm1 }

      TForm1 = class(TForm)
            Button1: TButton;
            TheImage: TImage;
            procedure Button1Click(Sender: TObject);
      procedure FormCreate(Sender: TObject);
      private

      public

      end;

var
      Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

var Bitmap:TBitmap;


procedure TForm1.FormCreate(Sender: TObject);
begin
  TheImage.left := 10;
  TheImage.top := 10;
  TheImage.width:=TheImage.Width - 20;
  TheImage.height:=TheImage.Height - 20;

  Bitmap := TBitMap.Create;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TheImage.Picture.Bitmap.canvas.pen.color := clBlack;
  TheImage.Picture.Bitmap.canvas.moveto(20,20);
  TheImage.Picture.Bitmap.canvas.lineto(100,50);

  TheImage.Picture.Graphic := Bitmap;
end;

end.

lainz

  • Hero Member
  • *****
  • Posts: 4738
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Simple line drawing program
« Reply #1 on: September 22, 2025, 09:53:23 pm »
Drop a TPaintBox, add OnPaint event, add this code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.PaintBox1Paint(Sender: TObject);
  2. begin
  3.   PaintBox1.Canvas.Line(0,0,PaintBox1.Width,PaintBox1.Height);
  4. end;

PascalDragon

  • Hero Member
  • *****
  • Posts: 6235
  • Compiler Developer
Re: Simple line drawing program
« Reply #2 on: September 22, 2025, 10:00:15 pm »
unit Unit1;

Please use [code=pascal][/code]-tags to make the code better viewable and to avoid the forum software potentially interpreting your code.

speter

  • Sr. Member
  • ****
  • Posts: 452
Re: Simple line drawing program
« Reply #3 on: September 23, 2025, 01:23:55 am »
Firstly, I totally agree with Iainz: use a standard canvas; unless you have some compelling reason...

But, the following works (see attached project):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button_image_drawClick(Sender: TObject);
  2. begin
  3.   with image1 do
  4.     begin
  5.       canvas.brush.color := clwhite;
  6.       canvas.pen.color := canvas.brush.color;
  7.       canvas.rectangle(0,0, width,height);
  8.  
  9.       canvas.pen.width := 5;
  10.       canvas.pen.color := clblack;
  11.       canvas.line(20,20, width-20,height-20);
  12.     end;
  13. end;

The line on the left is drawn on an image; the one on the right is on a standard canvas.

cheers
S.

PS: In your program, you draw on "TheImage.Picture.Bitmap", then replace what you have done by assigning the empty "Bitmap" to the image (you create the Bitmap, but have not drawn on it).
« Last Edit: September 23, 2025, 01:26:30 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

jamie

  • Hero Member
  • *****
  • Posts: 7405
Re: Simple line drawing program
« Reply #4 on: September 23, 2025, 01:39:46 am »
Keep in mind that the rectangle function and related functions that accept 4 parameters to form the square has a bug in it.

That is, the Right and lower right are supposed to be exactly what you want the box as.

When using these functions that accept TRECT parameters, what you have there would be correct, because with a TRect record the Right and lower Right are always one more than the actual surface area.

 But the Lcl code using the four parameters simply calls the TRECT version in the background without making corrections.

 This may get fixed one day so I would advise to lean towards using the TRECT versions for now.

Jamie
 
The only true wisdom is knowing you know nothing

Neville

  • Jr. Member
  • **
  • Posts: 52
Re: Simple line drawing program
« Reply #5 on: September 23, 2025, 10:19:47 am »
Many thanks to all contributors to my question.  It's been very helpful, and gets me going.

N

 

TinyPortal © 2005-2018