You can draw on these four drawing areas with Cairo:
You can draw the following geometric shapes (lines or surfaces or text) on the drawing area:
To draw text, you must specify the text, the font information, the font colour and the (start) point at which the font is inserted into the drawing area after calling special methods.
All drawing areas have a (virtual) coordinate system to which all coordinate specifications in the properties or in the arguments of the methods of the Cairo class refer. Please observe these notes:
Figure 25.1.10.3.1: Cairo coordinate system with shifted coordinate origin
In order to draw the designations of the two axes and the values in a way that is easy to read if required, you can (permanently) move the coordinate origin of the coordinate system using the Translate method of the Cairo class. However, this shift does not change the direction of the two coordinate axes!
Cairo.Begin(hImage) Cairo.Translate(40, 45) '-- Shift by +40.0 in the x-direction and by +45.0 in the y-direction ... Cairo.End()
Figure 25.1.10.3.2: Image of a function y = f(x)
To realise the usual view in contrast to → Figure 25.1.10.3.1, you can scale the coordinate system so that the positive y-axis points upwards. A parallel shift of the coordinate origin O(0|0), combined with a mirroring of the y-axis at the abscissa, realised by using the scale method, is implemented in this way:
Cairo.Begin(hImage) Cairo.Translate(40, 45) '-- Shift by +40.0 in the x-direction and by +45.0 in the y-direction Cairo.Scale(1, -1) '-- The -1 causes the direction of the y-axis to be inverted → +y ▲ ... Cairo.End()
The concept for drawing geometric shapes with Cairo is similar to drawing with Paint. Differences are described in section → 25.1.10.7. A geometric shape is always drawn in three steps on the drawing area used:
Example: A point cloud is drawn on an image (object) as a drawing area:
Public Sub CairoScriptPointCloud() Dim i As Integer GenerateNewImage() SetImageBorder() Cairo.Begin(hImage) Cairo.Translate(xTranslate, yTranslate) Cairo.Scale(xScale, yScale) ' +y ▲ Cairo.AntiAlias = False DrawCoordinateSystem() For i = 1 To 50000 '-- Set points with random coordinates and a random colour SetPoint(Rnd(10, 550 - 5), Rnd(10, 260), Color.RGB(Rnd(0, 255), Rnd(0, 255), Rnd(0, 255))) Next Cairo.AntiAlias = True Cairo.End() End
To see the result of the drawing, the image - which is currently only in memory - is drawn in a DrawingArea (daCanvas) and thus becomes visible:
Public Sub daCanvas_Draw() If hImage Then Paint.DrawImage(hImage, 0, 0, hImage.W, hImage.H) End
This flexible way of drawing has proven its worth. If you want to save the drawn image and process it further, for example to present it in a text, save the image in an image file:
Public Sub btnSaveCurImage_Click() hImage.Save(sImagePath &/ Lower(cmbImages.Text & ".png")) End
Figure 25.1.10.5.1: Point cloud
In chapter → 25.1.11 Cairo projects, further image examples are presented and commented on in detail.
There are (default) start values for drawing with Cairo:
sans-serif
. A test results in these values:
Print Cairo.LineWidth '-- 2 Print Cairo.AntiAlias '-- 0 Print Cairo.Font.Size '-- 10
You should take a very close look at the documentation for the properties and methods of the Cairo class of the Cairo component and the Paint class (gb.qt4), as some methods have the same names but different syntax or, for example, the same properties have different value ranges. Particular attention should be paid to the following topics