Lazarus - Chapter 09

Embed Size (px)

Citation preview

  • 7/28/2019 Lazarus - Chapter 09

    1/2

    Chapter 9Graphics Programming

    By Felipe Monteiro de Carvalho

    When we speak about graphics, we could be referring to two separate topics.On the one hand we may be talking about the manipulation of pictures or images how to handle existing graphic data. On the other hand, we may be thinking

    about how to draw on a canvas and display a picture. These are related andoverlapping topics.Lazarus offers very powerful tools for both these activities. The control canload graphics in several formats from files and resources; and various visualcontrols together with the TCanvas class provide a drawing surface, which can bemanipulated and drawn upon.

    LCL

    TImage

    canvas canvas

    TBitmapCanvas

    TPoint TRect TSize

    9.1 The CanvasThe is the basis for all drawing operations. The is a property of visual

    components and it is derived from graphics classes such as . Being a classproperty has no memory of its own. That is why an error occurs if you try to usea canvas before you have instantiated a visual control for it. The takes care of thenecessary preparations for drawing visual components on a form, and for displayingvisual components onscreen.Drawing on a canvas is always done using X,Y co-ordinates. The co-ordinate system'sorigin is always at the top left corner of the working area. The window's title bar isoutside this area, so it is not affected. Drawing is done either using separate X and Yinteger co-ordinates, or using co-ordinates which are grouped together as records, eitheras a (a record linking an X and a Y co-ordinate) or a . Some graphic methodsdo not work with absolute co-ordinates, but with a record instead. Such a recordonly defines the width and height, but not the target position.

    Lazarus - the complete guide560

  • 7/28/2019 Lazarus - Chapter 09

    2/2

    9.1 The Canvas

    = record

    : ;

    : ;

    end;

    = ;

    = record

    case of

    0: ( , , , : );

    1: ( , : );

    end;

    = ;

    = record

    : ;

    : ;

    end;= ;

    procedure . ( : ; : ;

    : ; , : );begin

    . ( , );

    . (100, 100);

    end;

    TPoint

    X LongInt

    Y LongInt

    PPoint ^TPoint

    TRect

    Integer

    Left Top Right Bottom LongInt

    TopLeft BottomRight TPoint

    PRect ^TRect

    TSize

    cx LongInt

    cy Longint

    PSize ^TSize

    TForm1 FormMouseDown Sender TObject Button TMouseButton

    Shift TShiftState X Y integer

    Canvas MoveTo x y

    Canvas LineTo

    The real declaration is more complex. It ensures that the type declarations are identical inthe units , and . However, the result shown above is how developerssee it (and should see it). A little example will show how simple the philosophy behindLazarus drawing operations is. We create a new Lazarus application and extend the

    event of the window with just two lines:

    After a mouse press on the form X and Y will contain the actual X and Y co-ordinates.The drawing tool is moved to that location and then a line is drawn to thepre-programmed location (100, 100) in the window. This drawing operation is done onthe window canvas. This will always happen, unless a different control's canvas isspecified. This work is volatile. If the window is updated (for instance when its sizechanges) the lines drawn earlier will disappear.

    The canvas is capable of many more operations than this simple one.The most important methods and properties in the following definition are brieflyexplained in Table 9.1. We will see more in the examples that follow later.

    As the work done on the form's canvas is not retained, the graphic data will have to bepainted again after every update of the control. For the graphic to persist, therefore,

    we will have to keep a list ourselves of the completed drawing operations in a buffer.

    Classes Types Windows

    FormMouseDown

    Lazarus - the complete guide 561