Forum > Other

Why 'TAgg2Dobj.attach(image,true)' always return false (fpgui)?

(1/1)

vkhoa:
Just found this room,:)
I have a small fpgui program like this

--- Code: ---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.
         
--- End code ---
Notice the line I commented above , please explain me why it return false?

vkhoa:
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

Graeme:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  ac.ClearAll (255 ,255 ,255 );  // clear the AggPas canvas to color white  ac.LineWidth(3);  // set the line width  ac.LineColor($32 ,$cd ,$32 );  // set the line color 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.
 

vkhoa:
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:
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.

Navigation

[0] Message Index

Go to full version