Você está na página 1de 2

Chapter 9 Graphics 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 and overlapping topics. Lazarus offers very powerful tools for both these activities. The TImage control can load graphics in several formats from files and resources; and various visual controls together with the TCanvas class provide a drawing surface, which can be manipulated and drawn upon.

9.1 The Canvas

The canvas is the basis for all drawing operations. The canvas is a property of visual components and it is derived from graphics classes such as TBitmap. Being a class property Canvas has no memory of its own. That is why an error occurs if you try to use a canvas before you have instantiated a visual control for it. The LCL takes care of the necessary preparations for drawing visual components on a form, and for displaying visual components onscreen. Drawing on a canvas is always done using X,Y co-ordinates. The co-ordinate system's origin is always at the top left corner of the working area. The window's title bar is outside this area, so it is not affected. Drawing is done either using separate X and Y integer co-ordinates, or using co-ordinates which are grouped together as records, either as a TPoint (a record linking an X and a Y co-ordinate) or a TRect. Some graphic methods do not work with absolute co-ordinates, but with a TSize record instead. Such a record only defines the width and height, but not the target position. 560

Lazarus - the complete guide

9.1 The Canvas

TPoint = record X: LongInt; Y: LongInt; end; PPoint = ^TPoint; TRect = record case Integer of 0: (Left, Top, Right, Bottom: LongInt); 1: (TopLeft, BottomRight : TPoint); end; PRect = ^TRect; TSize = record cx : LongInt; cy : Longint; end; PSize = ^TSize;

The real declaration is more complex. It ensures that the type declarations are identical in the units Classes, Types and Windows. However, the result shown above is how developers see it (and should see it). A little example will show how simple the philosophy behind Lazarus drawing operations is. We create a new Lazarus application and extend the FormMouseDown event of the window with just two lines:
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: integer); begin Canvas.MoveTo(x, y); Canvas.LineTo(100, 100); end;

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 the pre-programmed location (100, 100) in the window. This drawing operation is done on the window canvas. This will always happen, unless a different control's canvas is specified. This work is volatile. If the window is updated (for instance when its size changes) 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 briefly explained 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 be painted 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.

Lazarus - the complete guide

561

Você também pode gostar