Adding objects to the canvas fom within the mousePressed() method

Your post is extracted from this reference:
https://processing.org/reference/draw_.html

It is good etiquette to show the source of a quote.
There is a lot more context and details in the reference.

:)