孤独的猫

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

turbo pascal 7.0的单元引用与类设计

下面在turbo pascal7中定义了一个单元,里面声明并实现了几个画图类:

unit Figures;
interface
uses Graph,Crt;
type
  Location=object
    X,Y:integer;
    procedure Init(InitX,InitY:Integer);
    function GetX:Integer;
    function GetY:Integer;
end;

PointPtr=^Point;
Point=object(Location)
    Visible:Boolean;
    constructor Init(InitX,InitY:Integer);
    destructor Done;virtual;
    procedure Show;virtual;
    procedure Hide;virtual;
    function IsVisible:boolean;
    procedure MoveTo(NewX,NewY:Integer);
    procedure Drag(DragBy:Integer);virtual;
  end;
  CirclePtr=^Circle;
  Circle=object(Point)
    Radius:Integer;
    constructor Init(InitX,InitY:Integer;InitRadius:Integer);
    procedure Show;virtual;
    procedure Hide;virtual;
    procedure Expand(ExpandBy:Integer);virtual;
    procedure Contract(ContractBy:Integer);virtual;
  end;

  implementation
  procedure Location.Init(InitX,InitY:Integer);
  begin
    X :=InitX;
    Y :=InitY;
  end;

  function Location.GetX:Integer;
  begin
    GetX :=X;
  end;

  function Location.GetY:Integer;
  begin
    GetY :=Y;
  end;

  constructor Point.Init(InitX,InitY:integer);
  begin
    Location.Init(InitX,InitY);
    Visible :=False;
  end;

  destructor Point.Done;
  begin
    Hide;
  end;

  procedure Point.Show;
  begin
    Visible :=True;
    PutPixel(X,Y,GetColor);
  end;

  procedure Point.Hide;
  begin
    Visible :=False;
    PutPixel(X,Y,GetBkColor);
  end;

  function Point.IsVisible:Boolean;
  begin
    Visible :=Visible;
  end;

  procedure Point.MoveTo(NewX,NewY:Integer);
  begin
    Hide;
    X :=NewX;
    Y :=NewY;
    Show;
  end;

  function GetDelta(var DeltaX:Integer;var DeltaY:Integer):boolean;
  var
    KeyChar:Char;
    Quit:Boolean;
  begin
    DeltaX :=0;
    DeltaY :=0;
    GetDelta :=True;
    repeat
      KeyChar :=ReadKey;
      Quit :=True;
      case Ord(KeyChar) of
        0:  begin
              KeyChar :=ReadKey;
              case Ord(KeyChar) of
                72:  DeltaY :=01;
                80:  DeltaY :=1;
                75:  DeltaY :=-1;
                77:  DeltaY :=1;
              else
                Quit :=False;
              end;
            end;
        13: GetDelta :=False;
        else
          Quit :=False;
      end;
    until Quit;
  end;

  procedure Point.Drag(DragBy:Integer);
  var
    DeltaX,DeltaY:Integer;
    FigureX,FigureY:Integer;
  begin
    Show;
    FigureX :=GetX;
    FigureY :=GetY;
    while GetDelta(DeltaX,DeltaY) do
    begin
      FigureX :=FigureX+(DeltaX*DragBy);
      FigureY :=FigureY+(DeltaY*DragBy);
      MoveTo(FigureX,FigureY);
    end;
  end;


  constructor Circle.Init(InitX,InitY:Integer;InitRadius:Integer);
  begin
    Point.Init(InitX,InitY);
    Radius :=InitRadius;
  end;

  procedure Circle.Show;
  begin
    Visible :=True;
    Graph.Circle(X,Y,Radius);
  end;

  procedure Circle.Hide;
  var
    TempColor:Word;
  begin
    TempColor :=Graph.GetColor;
    Graph.SetColor(GetBkColor);
    Visible :=False;
    Graph.Circle(X,Y,Radius);
    Graph.SetColor(TempColor);
  end;

  procedure Circle.Expand(ExpandBy:Integer);
  begin
    Hide;
    Radius :=Radius+ExpandBy;
    if Radius<0 then Radius :=0;
    Show;
  end;

  procedure Circle.Contract(ContractBy:Integer);
  begin
    EXpand(-ContractBy);
  end;


end.

在相同目录下建一个工程引用上面这个单元文件

program makePoints;
uses Graph,Figures;
var
  APoint:Point;
  GraphDriver,GraphMode:Integer;

begin
  GraphDriver :=Detect;
  InitGraph(GraphDriver,GraphMode,'D:\TP\BGI');
  APoint.Init(151,82);
  APoint.Show;
  APoint.MoveTo(163,101);
  APoint.Hide;
  Readln;
  CloseGraph;
end.

posted on 2011-03-02 21:10  孤独的猫  阅读(301)  评论(0)    收藏  举报