Recent

Author Topic: Canvas.SetColor error: identifier idents no member "SetColor"?  (Read 3301 times)

JoshM

  • Jr. Member
  • **
  • Posts: 57
Code: [Select]
{$mode objfpc}{$h+}
program demo;

uses classes, sysutils,
     FPImage, FPCanvas, FPImgCanv,
     FPWritePNG, Graphics;

var canvas : TFPCustomCanvas;
    image : TFPCustomImage;
    writer : TFPCustomImageWriter;
    red, white : TFPColor;
    x, y : integer;
begin
  { Create an image 100x100 pixels}

  Red.red:=65535;
  Red.green:=0;
  Red.blue:=0;
  Red.alpha:=65535;

  white.red := 65535;
  white.blue := 65535;
  white.alpha :=65535;
  white.green := 65535;

  image := TFPMemoryImage.Create (100,100);


  { Attach the image to the canvas }
  Canvas := TFPImageCanvas.create(image);

  { Create the writer }
  Writer := TFPWriterPNG.Create;

  for y := 0 to 100 do begin
  for x := 0 to 100 do begin
     If x DIV 2 = 0 then
     Canvas.SetColor(x, y, red) //error here
     else
     Canvas.SetColor(x, y, white); //error here
  end;
  end;

  { Save to file }
  image.SaveToFile ('C:\Users\Josh.PC1\Desktop\DrawTest.png');

  { Clean up! }
  Canvas.Free;
  image.Free;
  writer.Free;
end.

Does anyone know what I'm doing wrong? It's my first time working with the canvas and I've literally just copied the basic code from here (http://wiki.freepascal.org/fcl-image#Basic_Canvas_Setup). Not sure if this will help, but when I type canvas. (ready for all the function/procedure suggestions to appear), SetColor does not when I think it should. At first I kept getting "Graphics unit not found" but I seem to have partly fixed that at least.

Any help would be appreciated :)

Blaazen

  • Hero Member
  • *****
  • Posts: 2852
  • POKE 54296,15
    • Eye-Candy Controls
Re: Canvas.SetColor error: identifier idents no member "SetColor"?
« Reply #1 on: July 03, 2013, 10:16:02 pm »
AFAIK Canvas has no method SetCanvas. Instead, you can use:
Code: [Select]
Canvas.Pixels[x, y] := clRed;

But painting whole picture this way will be slow. Maybe, your algorithm can be substituted with vertical lines ?
Lazarus 2.1.0 r63594 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

howardpc

  • Hero Member
  • *****
  • Posts: 3487
Re: Canvas.SetColor error: identifier idents no member "SetColor"?
« Reply #2 on: July 03, 2013, 11:41:34 pm »
SetColor is a protected method of TFPImageCanvas, which as Blaazen suspected simply uses the slow Pixels[] property.
In order to get working code you have to adapt the example you found as follows.
(The example is old and refers to earlier FPC/Lazarus versions which have since changed. The code nmtv posted differs from what is currently shown on the referenced wiki page).

Code: [Select]
{$mode objfpc}
program demo;

uses FPImage, FPWritePNG, FPImgCanv;

type

   { TFPImgCanvasEx }

   TFPImgCanvasEx = class(TFPImageCanvas)
   public
     procedure SetColor(x, y: integer; const AValue: TFPColor); override;
   end;

var canvas : TFPImgCanvasEx;
    image : TFPCustomImage;
    writer : TFPCustomImageWriter;
    red, white : TFPColor;
    x, y : integer;

{ TFPImgCanvasEx }

procedure TFPImgCanvasEx.SetColor(x, y: integer; const AValue: TFPColor);
begin
  inherited SetColor(x, y, AValue);
end;

begin
  { Create an image 100x100 pixels}

  Red.red:=65535;
  Red.green:=0;
  Red.blue:=0;
  Red.alpha:=65535;

  white.red := 65535;
  white.blue := 65535;
  white.alpha :=65535;
  white.green := 65535;

  image := TFPMemoryImage.Create (100,100);


  { Attach the image to the canvas }
  Canvas := TFPImgCanvasEx.create(image);

  { Create the writer }
  Writer := TFPWriterPNG.Create;

  for y := 0 to 100 do begin
  for x := 0 to 100 do begin
     If x DIV 2 = 0 then
     Canvas.SetColor(x, y, red) //there was an error here previously
     else
     Canvas.SetColor(x, y, white);
  end;
  end;

  { Save to file }
  image.SaveToFile ('DrawTest.png');

  { Clean up! }
  Canvas.Free;
  image.Free;
  writer.Free;
end.


« Last Edit: July 03, 2013, 11:47:40 pm by howardpc »

JoshM

  • Jr. Member
  • **
  • Posts: 57
Re: Canvas.SetColor error: identifier idents no member "SetColor"?
« Reply #3 on: July 04, 2013, 09:24:22 pm »
Thanks for the solution. I read about the SetColor procedure here:
http://lazarus-ccr.sourceforge.net/docs/lcl/graphics/tcanvas.setcolor.html

To me that looks as though I can type Canvas.SetColor It's probably just that I'm wrong, but maybe it could be an outdated article...?

 

TinyPortal © 2005-2018