delphidelphi-7delphi-units

Delphi canvas figures


i got some homework on Dephi (never used it before, only c++/java but in my universuty we've got delphi language subject). Well, i need to make form with moving figures, shown how they collides and stuff like. I started to make a uint like some abstarct class

unit MyFigure;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Buttons, StdCtrls;

type
tPoint = record
 x,y: Double;
end;

oFigure = class
  c: TCanvas;
  pos: tPoint;
  vel: tPoint;
  r: Double;
  constructor create(coord, vect: tPoint; radius: Double);
  protected
    procedure move();
    procedure draw(); virtual;
  public
    function isIntersected(x:oFigure):boolean;
end;

implementation

  constructor oFigure.create(coord, vect: tPoint; radius: Double);
  begin
    pos.x:= coord.x;
    pos.y:= coord.y;
    vel.x:= vect.x;
    vel.y:= vect.y;
    r:=radius;
  end;

  procedure oShape.draw(); virtual;
  begin

  end;

  procedure oShape.move();
  begin
      pos.x:=  pos.x + vel.x;
      pos.y:=  pos.y + vel.y;
      oShape.draw();

  end;

  function isIntersected(o:oFigure):boolean;
  begin
     if ((oShape.pos.x - o.pos.x)*(oShape.pos.x - o.pos.x) +  (oShape.pos.y - o.pos.y)*(oShape.pos.y - o.pos.y)
          < (oShape.r + o.r)*(oShape.r + o.r)) then Result:=True;
  end;



end.

Then I created it's child. Well, here i need to call arc method from canvas to draw ball, but it don't see it and eve says unable to invoke code completion. Whats wrong?

unit Ball;

interface
uses
  MyFigure;   
type

oBall = class(oFigure);
    c: TCanvas;
    procedure draw(); override;
 end;

implementation
   procedure oBall.draw();
    begin
       c.Arc()//PROBLEM!
    end;

end.

Solution

  • The Code completion is not invoked because the unit graphics is not specified in the uses clause: Try with

    unit Ball;
    
    interface
    uses
      Graphics, MyFigure;
    

    By the way you don't seem to instanciate c. You'd need a constructor and a destructor for this. The usuall way would be to pass a TCanvas instance as parameter in the procedure draw(). In the unit oFigure, You define TPoint but TPoint is a native type of the RTL/VCL. You don't need to to define it. In oFigure you also set some methods as protected but paradoxycally the previous variables are public.