Graphics Context - Introduction

The Graphics Context

When generating shapes there is a separation between the geometry of the shape and the details of the rendition. The code within the shape routine controls the geometry while the context controls the colours, line styles etc.

The graphics context objects can be split into two groups. The first two, Origin and Canvas, determine where the shape is drawn and the other three, Pen, Brush and Font, determine how it is drawn.

When a program starts the system will set up an initial context, based partly on the command line arguments, which can be overridden within the program by modified versions. This overriding will be in force within a certain scope and when that scope ends the previous context is restored. This can in principle go on for an indeterminate number of levels. The context can be overridden by specifying the new context in the drawing call or within a routine using the With statement.

The system maintains the context, including freeing any resources when a context object is no longer required.


In this example a standard shape, Circle(), is invoked twice. The first time using the context passed to it by the caller. The second time the pen is overridden by a temporary pen based on the inherited pen but with the colour explicitly changed to red.

	Circle(radius + 5) => Pen { colour -> RED };
gctx_intro.grs Output of gctx_intro.grs