home/ atoms/ processing-state-based-drawing

Processing maintains drawing style state until explicitly changed

Processing is state-based: calling stroke(), fill(), strokeWeight(), and similar style functions sets a persistent state that applies to all subsequent drawing commands until changed. This means you can set style once before a group of shapes and they all inherit it, but you must explicitly reset styles before drawing shapes with different appearances. A common bug is drawing a shape with the wrong style because an earlier state is still in effect. The order of execution also determines layering: shapes drawn later appear on top.

Examples

stroke(255,0,0); strokeWeight(4); line(…); // red thick line. stroke(0,255); strokeWeight(1); ellipse(…); // thin black ellipse. If you forget the second stroke() call, the ellipse uses red too.

Assessment

Given a Processing sketch that draws a thick red rectangle and a thin blue circle, describe what happens if the stroke() and fill() calls are placed before both shapes versus between them.

“Processing is state-based, which means that when you set a style (such as the color or weight of the stroke), that state will be maintained until you set it to something else.”
corpus · generative-art-a-practical-guide-using-processing-matt-pears · chunk 12