AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Contents · Computing

Graphics in Delphi

Lessons 59–60 · 2 lessons · N. I. Taylaqov (general editor), A. B. Akhmedov, M. D. Pardayeva, A. A. Abdug‘aniyev, U. M. Mirsanov. Informatics and Information Technologies, Grade 10, 1st edition. “Extremum-Press”, Tashkent, 2017
59

Working with graphics in Delphi

Textbook: pp. 132–137
GoalDraws on an Image object with the Canvas: uses the Pen and Brush properties, the Rectangle, Ellipse, MoveTo and LineTo methods, the Pixels property and colours (cl names, RGB, $BBGGRR).
New words
Canvas: the drawing surface of an object such as an Image · CanvasPen / Brush: the pen draws lines, the brush fills areas · Pen / BrushRGB: a function that makes a colour from red, green and blue values 0–255 · RGBPixels: the colours of single dots of the canvas, given by two coordinates · Pixels
Explanation

Many objects have a Canvas property: shapes are drawn on it. For drawing it is convenient to use the Image object from the Additional tab, because a shape drawn on it is not lost when the window is covered and shown again (a shape drawn directly on Form1.Canvas can disappear when the window is covered by another). Coordinates are counted from the upper left corner of the canvas: x – to the right, y – down. Rectangle(x1, y1, x2, y2) draws a rectangle and Ellipse(x1, y1, x2, y2) an ellipse inscribed in that rectangle; an ellipse in a square is a circle. MoveTo(x, y) moves the pen to a point and LineTo(x, y) draws a segment from the current point to the given point. The Pen (Color, Width, Style) draws lines and the Brush (Color, Style) fills the inner area. A colour is given with cl names (clRed, clBlue, clYellow) or with RGB(red, green, blue), each value from 0 to 255, so there are 256·256·256 = 16777216 colours. One dot can be coloured with Canvas.Pixels[x, y] := RGB(255, 0, 0); In Lazarus use RGBToColor(r, g, b) instead of RGB.

ColourRGB
yellow(255, 255, 0)
blue(0, 0, 255)
black(0, 0, 0)
white(255, 255, 255)
💡 Where will I use this?
In real life

A pupil drew a school map on squared paper and described the library with just two corners: from 2 across and 3 down to 6 across and 5 down. With these corner numbers anyone can draw the same rectangle. A computer also draws a shape from its corner coordinates, counting from the top left corner.

Who uses it

Game programmers, graphic designers and map makers define shapes exactly with coordinates.

Try it today

On squared paper choose a rectangle by the coordinates of two corners and give a friend only those numbers to see if they can draw the same rectangle.

Worked examples
Pascal
1procedure TForm1.Button1Click(Sender: TObject);
2begin
3  Image1.Canvas.Rectangle(40, 80, 140, 160);
4  Image1.Canvas.MoveTo(40, 80);
5  Image1.Canvas.LineTo(90, 40);
6  Image1.Canvas.LineTo(140, 80);
7end;
A house: Image1 (300×200) – code above. The wall is 140–40 = 100 pixels wide and 160–80 = 80 high; the roof peak is at x = (40+140)/2 = 90.
The semi-axes of Ellipse(10, 40, 70, 120) are (70–10)/2 = 30 and (120–40)/2 = 40. A sun: Image1.Canvas.Brush.Color := clYellow; Image1.Canvas.Ellipse(220, 20, 260, 60); – a circle in a 40×40 square. RGB(255, 255, 0) is yellow, RGB(0, 0, 255) is blue, RGB(0, 0, 0) is black and RGB(255, 255, 255) is white.
Class activity

“House and sun”: a pair draws a house, a door and a sun with their own coordinates; they calculate the width and height of the wall in advance and compare them with the sizes in the picture.

Practice
1
Where are the Canvas coordinates counted from?
2
What are the width and height of Rectangle(30, 50, 130, 150)?
3
What are the semi-axes of Ellipse(10, 20, 90, 60)?
4
Why is drawing on an Image more reliable than drawing on Form1.Canvas?
Story of the Week · The Khwarizmi WayCurrent in a vacuum? Grandpa’s radioClose the week: all of this week’s subjects in one storyRead →