Recent

Author Topic: Why 'TAgg2Dobj.attach(image,true)' always return false (fpgui)?  (Read 5803 times)

vkhoa

  • New Member
  • *
  • Posts: 47
Why 'TAgg2Dobj.attach(image,true)' always return false (fpgui)?
« on: November 30, 2014, 02:15:21 pm »
Just found this room,:)
I have a small fpgui program like this
Code: [Select]
program TryAgg2dAttach;
uses classes,
  fpg_gdi,
  fpg_base,
  fpg_main,
  fpg_form,
  fpg_widget,
  Agg2D;
type
mwidget=class(tfpgwidget)

   constructor create(acom:tcomponent);
   procedure   HandlePaint;override;
   private
      img:tfpgimage;
end;

TMainForm = class(TfpgForm)
   mywidget:mwidget;
   procedure   AfterCreate; override;

 end;
constructor mwidget.create(acom:tcomponent);
var ac:tagg2d;
begin
   inherited create(acom);
   img:=tfpgimage.Create;
   ac:=tagg2d.Create(self);
   if ac.Attach(img,true) then begin //       <-------this always return false
      ac.Curve(0,0,50,50,100,0,150,50);
   end;
end;
procedure   mwidget.HandlePaint;
begin
   canvas.clear(clwhite);
   canvas.DrawImage(0,0,img);
   canvas.DrawArc(30,30,50,50,0,135);
end;

procedure   tmainform.AfterCreate;
begin
   width:=300;height:=100;
  mywidget:=mwidget.create(self);
  mywidget.SetPosition(0,0,300,100);
end;
procedure MainProc;
var
  frm: TMainForm;
begin
  fpgApplication.Initialize;
  frm := TMainForm.Create(nil);
  frm.Show;
  fpgApplication.Run;
  frm.Free;
end;

begin
  MainProc;
end.
         
Notice the line I commented above , please explain me why it return false?
« Last Edit: December 01, 2014, 09:05:25 am by vkhoa »

vkhoa

  • New Member
  • *
  • Posts: 47
Re: Why 'TAgg2Dobj.attach(image,true)' always return false (fpgui)?
« Reply #1 on: December 01, 2014, 09:40:56 am »
It returned true after I had added the code
'img.AllocateImage(32,400,300);' after 'img:=tfpgimage.Create;'
notice the first parameter of allocateimage must be 32
but the curve was still not shown
« Last Edit: December 01, 2014, 09:45:23 am by vkhoa »

Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Re: Why 'TAgg2Dobj.attach(image,true)' always return false (fpgui)?
« Reply #2 on: February 28, 2015, 02:45:16 am »
You were almost there.  :)  You simply forgot to initialize the line used to draw the curve.

After the ac.Attach() call, and before you call ac.Curve() you had to add the following lines of code:

Code: Pascal  [Select]
  1.   ac.ClearAll (255 ,255 ,255 );  // clear the AggPas canvas to color white
  2.   ac.LineWidth(3);  // set the line width
  3.   ac.LineColor($32 ,$cd ,$32 );  // set the line color
  4.  
By default the AggPas canvas is black (RGB values of 0, 0, 0) and the line color is black - hence you didn't see anything.

Attached is a slightly extended version of your sample code. The changes I made are as follows:
 
  • I correctly initialized the AggPas canvas and line properties
  • Added a checkbox to show or hide the actual co-ordinates used for the bezier curve
  • You flipped the Y co-ordinate of the AggPas canvas in the ac.Attach() call. Not sure if that
    was by design. But I added another checkbox to toggle that option

The final result can be seen in the attached screenshot. I've also attached the modified source code.

TIP:
  You never need to specific fpg_gdi or fpg_x11 in the uses clause.
 
« Last Edit: July 20, 2016, 12:57:43 pm by Graeme »
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

vkhoa

  • New Member
  • *
  • Posts: 47
Re: Why 'TAgg2Dobj.attach(image,true)' always return false (fpgui)?
« Reply #3 on: February 28, 2015, 06:32:18 am »
Great, it worked
in my another sample code, I had initialized the line property, but the image was still not displayed
Reading your code, I found that my fault is that I didn't call "img.updateimage" after the paint.
thanks very much :)

Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Re: Why 'TAgg2Dobj.attach(image,true)' always return false (fpgui)?
« Reply #4 on: February 28, 2015, 11:27:11 am »
Your welcome. :)  UpdateImage() is required after you manually modify ImageData (which is what TAgg2D does too). Internally it creates the required OS resources that represents an image and sets up any image mask if needed.
« Last Edit: February 28, 2015, 11:29:10 am by Graeme Geldenhuys »
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/