What do I need the Drawing Context for?

New here - and new to Rhombus!

Via the pict module I generate little pictures. In the REPL they are displayed immediately.

Now I try to write the pictures to a file, so that I can run the Rhombus program from a normal shell as well (and not only from the REPL).

Say, I have my Pict in def p = .... Claude gave me the following suggestion to write it to a file:

def bm = draw.Bitmap([p.width, p.height], ~backing_scale: 2)
def bm_dc = bm.make_dc()
p.draw(bm_dc)
bm_dc.write("out.png", ~kind: #'png)

but this gives me a "no field or method write" error. And indeed, the Drawing Context doesn't have a write method; instead the Bitmap has one.

So

def bm = draw.Bitmap([p.width, p.height], ~backing_scale: 2)
def bm_dc = bm.make_dc()
p.draw(bm_dc)
bm.write("out.png", ~kind: #'png)

works. But I do not understand what I then need the Drawing Context for in the first place? (I tried removing the two bm_dc lines, but then I do'nt see anything.)

Thanks for any help!

(Static) Pict values can be thought as combined geometric objects. To get an image out of a Pict, Bitmap creates a blank canvas and then def bm_dc = bm.make_dc() prepares a drawing context, or a collection of bm-specific tools like pens and brushes which all draw on bm. Finally, p.draw(bm_dc) tells the p to draw itself (the collection of geometric objects) using the pens and brushes from bm_dc as a painting on the canvas.

An interesting point is that the drawing context is stateful and remembers which pen or which brush it is using. So, when a drawing context reacts to an instruction like draw-rectangle, it uses the color/style associated with the current pen/brush.

Here are some example programs of drawing contexts in Racket: 1 Overview.

The Drawing Context interface is more general—bitmaps are not the only things you can draw to!

There are other drawing contexts available to generate vector graphics formats, like SVG, PDF, and EPS, and the GUI libraries add Drawing Contexts for drawing on the screen.