CONTENTS - Click any link to jump to that slide. Every slide's footer has a "Contents" link that brings you back here. - The lesson overview, timing, and sources are in the notes on the next slide (the title slide). - Links work in the HTML version (GitHub Pages) and in a browser. They may not work in the VS Code preview.
Lesson overview: the second deck of Unit 2 (Drawing with Code), after intro-cs.md ("Intro to Computer Science"), which covers what CS is, abstraction, binary, and why we use Java and Processing. This deck is in five parts; teach them in order, since later parts build on earlier ones. Students work in pairs: one types, one reads the slides and spots mistakes. Switch roles every few slides (the notes say when). | Part | Topic | Time | | --- | --- | --- | | 1 | The Processing app: the editor, sketch folders, and saving a sketch in a GitHub repo | about half a period | | 2 | Processing basics: statements, syntax, comments, the canvas, and coordinates | about half a period | | 3 | Shapes: point(), line(), rect(), ellipse(), and triangle() | 1 period | | 4 | Colors: RGB, fill() and stroke(), transparency, drawing order, text(), the reference, and the Taijitu activity | 1-2 periods | | 5 | Debugging: three kinds of bugs, error messages, commenting out, and println() | about 1 period | The times are rough; adjust them for your class. Materials: Processing installed on student laptops (https://processing.org/download; may need IT) and graph paper for planning drawings. Objectives: by the end, students can draw with point(), line(), rect(), ellipse(), triangle(), and text(); color with fill(), stroke(), and transparency; save a sketch in their GitHub repo; and find and fix bugs by reading error messages, commenting out lines, and printing with println(). Follow-up: the first Processing assignment is U02A04 - Draw Your Game Character (gp/assignments/u2/), taught in Unit 2 between the two first-repository handouts. U02A05 - Code Detective follows it with more drawing practice, plus noFill() and noStroke(); Board Game in Progress (a working name) is another option. All three use only this deck. In U02A04, students plan a game character on paper, draw it with line(), rect(), and ellipse(), and save the sketch in a new GitHub repo. Next: variables.md ("Variables", Unit 3), then functions.md ("Functions", Unit 4). algorithms.md ("Algorithms") is unplugged and fits Unit 1. Source: adapted from CSE 120: Computer Science Principles, Winter 2020, University of Washington (instructor Sam Wolfson): "L04: Processing and Drawing." UW's course-specific slides (staff, policies, schedule, quizzes, assignments, news articles) were removed. The images and much of the wording come from that deck; UW course materials are not openly licensed, so this title slide credits the course; keep that line, since the deck is public on GitHub Pages. The GitHub steps, most of the practice slides, and the Debugging part were written for this class.
We'll use Processing for this course. Its syntax (the punctuation and word order the language requires; see "Syntax: punctuation pairs" in Part 2) is based on Java, but we're mainly using it because it's designed to create visual and interactive programs. Like I've been saying, the language doesn't really matter in general. Here are some cool things you'll be able to make! Both animations come from the UW course: a monster that follows the mouse, and a two-player tic-tac-toe game. Tic-tac-toe is a good preview of the board-game-to-digital projects later in GP1. Processing: https://processing.org/
What Processing is, the editor, how Processing saves a sketch as a folder, and how to save a sketch in a GitHub repo and commit it. The GitHub slides use the processing-sketches repo from U02A04 - Draw Your Game Character; see their notes.
Intro to CS (Part 5) covered why we use Java and Processing; this slide is just the practical part. There's lots of debate (and research papers) about what language to teach first. Processing isn't perfect, but it does enough things well. For this part, students use their laptops with a partner. Partner up now.
Demo: open Processing (make sure it's in Java mode). Show a line drawing: Run opens the drawing canvas, Stop closes it, and clicking Run again stops the old program and re-runs it. When you make a new program, Processing gives it a random name (like sketch_160325a) until you save it. The Debugger lets you problem-solve while the program runs; see the notes on "More debugging strategies" in Part 5.
Some quirks specific to Processing. Demo: the folder name must match the file name. If students move or rename one without the other, Processing complains ("The file needs to be inside a sketch folder named..."). GP1 tip: this matters when sketches go into a GitHub repo. Commit the whole folder, not just the .pde file.
Students make their processing-sketches repo on github.com in U02A04 - Draw Your Game Character, and clone it there. They need this slide again whenever they use a different computer, or one that's been reset: a sketch can only be saved into a repo folder that's on this computer. Same steps as Your First Repository, Part 1, Task 2, which cloned my-first-repo. The Local path is the folder students save into on the next slide, so have them write it down. It's usually Documents\GitHub\ plus the repo name; on some school computers it goes through OneDrive. The lock icon means the repo is private. If the repo isn't in the list: click the circle-arrows button next to the search box to refresh it, and check that GitHub Desktop is signed in to the student's own account. Screenshot: GitHub Desktop on Windows, Mr. Sidarous's own, cropped to the Clone window.
Use this slide when students start saving sketches to GitHub, with U02A04 - Draw Your Game Character (after the first repository handout). If you teach this deck before students have a repo (before Your First Repository, Part 1), skip it for now. Why it matters: Processing saves new sketches in its own sketchbook folder (Documents\Processing) unless you choose another folder. Git only sees files inside the repo folder, so a sketch saved anywhere else never shows up in GitHub Desktop. That's the most common problem in U02A04. Step 2: the window that opens is called "Save sketch folder as...". The repo folder is the Local path students wrote down when they cloned the repo. If they've forgotten it, Repository → Show in Explorer in GitHub Desktop opens the folder. On some school computers the path goes through OneDrive (OneDrive\Documents\GitHub). Step 3: students can go into the repo folder and type just the name, or type the whole path, as in the screenshot. Processing makes a folder with that name and puts the .pde inside it (see the Processing files slide), so GitHub Desktop lists the file inside that folder. To check where any sketch is saved, use Sketch → Show Sketch Folder. Shortcut: File → Preferences... → Sketchbook folder can point at the repo folder, so new sketches save there by default. Check whether the setting sticks on school computers before relying on it. Next: commit the sketch in GitHub Desktop (next slide). Screenshot: Processing 4.4.6 on Windows, Mr. Sidarous's own.
Step 4: GitHub Desktop shows one repo at a time. If Current repository says another repo (like my-first-repo), the new sketch won't be in Changes: click Current repository and pick the sketches repo. Step 5: the file appears inside its folder (game_character\game_character.pde), because Processing saved it as a sketch folder. Every line is green with a +, because the whole file is new. The screenshot has two sketches waiting: robot_warmup and game_character, from U02A04. Step 6: a good summary starts with an action word and says what changed, like Add game character (as in Your First Repository, Part 1). The button names the branch it commits to (main). Then Push origin uploads the commit to GitHub; until then, it's only on this computer. Screenshot: GitHub Desktop on Windows, Mr. Sidarous's own. An "updated version available" banner was cropped out.
Use this when a sketch doesn't show up in GitHub Desktop's Changes, or just to show students where their work lives. The address bar shows the path as steps (user name, Documents, GitHub, the repo). Clicking an empty spot in it shows the full path as text, like C:\Users\name\Documents\GitHub\processing-sketches, which matches the Local path from cloning. Each sketch is its own folder with the same name as its .pde file (see the Processing files slide). README.md is the file GitHub made when the repo was created on github.com. A repo made in GitHub Desktop instead may have a .gitattributes file and no README; both are normal, so leave them alone. The folder tree on the left shows other repos on the same computer: every cloned repo is its own folder inside Documents\GitHub. If a sketch is missing here, it was saved somewhere else, usually Processing's own sketchbook folder (Documents\Processing). Open it in Processing, use Sketch → Show Sketch Folder to see where it went, then File → Save As... into this folder. Screenshot: File Explorer on Windows 11, Mr. Sidarous's own.
The last check. Once the sketch is on github.com, it's backed up, and it's there for the teacher to see. Each row shows the summary of the commit that last changed it, and when. Clicking a folder shows its .pde file, and clicking the .pde shows the code. The Commits link opens the History: every commit, newest first, which is how the teacher checks progress (the exit tickets ask for the repo link for this reason). The Private label means only the student, and people they add, can see it. Not there? If the sketch is in the repo folder (previous slide) but not on github.com, it was committed but not pushed: GitHub Desktop's top bar shows Push origin with a number. If GitHub Desktop still lists it under Changes, it hasn't been committed yet. Screenshot: github.com, Mr. Sidarous's own. README.md was added after the sketches, so its commit is newer.
The rules every sketch follows: statements, syntax, comments, the canvas, and coordinates. Goal: students can predict what a line of code will do before they run it. Source: "L04: Processing and Drawing," CSE 120, Winter 2020, University of Washington. The syntax slide was written for this class.
A program is a list of statements that Processing runs from top to bottom. This whole deck uses plain statements like these (Processing calls this static mode), and so do the drawing assignments, starting with U02A04 - Draw Your Game Character. setup() and draw() come in the Variables deck (see "Active mode: setup() and draw()"). If a sketch has a void setup() { ... } and also lines outside it, Processing says "You may be mixing active and static modes." The fix: take the setup() wrapper out, or move every line inside it. Note that this is case-sensitive, so if you type "strokeweight" you'll get an error. You need a semicolon at the end of each statement so Processing knows where it ends. Color coding helps you understand what Processing thinks your code is doing. It's especially helpful when coding together: you can tell you made an error if the colors look wrong. The environment also shows where opening and closing parentheses match. Goal: understand what your code will do *before* you run it.
Syntax is like grammar and spelling: the computer won't guess what you meant. Every ( needs a ), every { needs a }, and every " needs a closing ". The semicolon is different: it marks the end of one statement, so it has no partner. A block is a group of statements wrapped in { }. The statements in a block belong together and run together, as a unit: when Processing runs setup(), it runs every line in setup()'s block, top to bottom. Students won't write blocks until the Variables deck, where they show up in setup() and draw(); later, if statements and loops use them too. Indent the lines inside a block (Processing uses two spaces) so you can see where it starts and ends; Edit → Auto Format (Ctrl+T) does it for you. Java, C#, C++, and JavaScript all use these same marks (Intro to CS deck, "Java looks like its cousins"), so this carries over to other languages.
Expectations for comments in this class (adjust to your own rubric): - A block comment at the top with the program name, your name, and a brief description - A brief comment above each part of the drawing (like // Head), and later above each function, saying what it does - A comment after any complicated line saying what it does Processing colors comments light gray. Comments help graders, partners, and you when you come back to the code a week later.
We call the window the "canvas" (UW's slides said "drawing canvas"; p5.js, the JavaScript version of Processing, calls it the canvas too). Processing's own reference and the Reas and Fry books call it the "display window" (for example, size() "Defines the dimension of the display window"), so point that out: students will see that term as soon as they look something up. Students follow along: 1. Type size(500, 500); and click Run. 2. Delete the semicolon. Will it run? (No: an error.) 3. Change to size(500, 800). Ask: "Will this canvas be tall or wide?" (Tall.) Run. 4. Change back to size(500, 500).
How do we specify positions on the canvas? In math, the origin is usually in the middle, with positive and negative x and y. For computers, it's handier for the origin not to move: it's always the upper-left corner, y increases top to bottom, and there are no negative coordinates on screen. This trips up almost everyone at first. Ask: "If I increase y, does the shape move up or down?"
The five basic shapes, each with a grid diagram, then practice. Pairs: one types, one reads the slides and spots mistakes. Source: "L04: Processing and Drawing," CSE 120, Winter 2020, University of Washington.
The simplest shape: one dot, at one (x, y). 1. Add point(250, 250); and Run. Ask: "Can you see it?" (Barely: it's one pixel.) 2. Points are drawn with the outline settings, so they get their color from stroke() and their size from strokeWeight(), which come up on the color slides. fill() does nothing to a point. A line connects two points, which is the next slide. Picture: drawn to match the other grid diagrams. Source: Processing Reference, point(), https://processing.org/reference/point_.html (CC BY-NC-SA 4.0).
Specify two points and connect them. 1. Add line(30, 60, 150, 60); and Run. 2. Add line(300, 300, 100, 100); Ask: "Will this show up above or below the first line?" (Below.) Run. 3. Ask: "Does the order of the points matter?" (No.) Change to line(100, 100, 300, 300); and Run. Switch partners: the typer becomes the spotter.
1. Add rect(100, 250, 175, 125); and Run. Ask: "Why does this look this way?" (The rect is on top of the line because its statement ran after.) 2. Swap the line and rect statements and Run: now the line is on top. 3. Ask: "There's no square function. Why not?" (A square is a special case of a rectangle.) Change to rect(100, 250, 125, 125);. (Newer versions of Processing also have square() and circle(), but rect() and ellipse() work everywhere.)
The difference from rect: ellipse uses the center, not the corner (which makes sense, since the corner isn't part of the ellipse). 1. Add ellipse(100, 250, 125, 125);. Ask: where will this show up relative to the square, with the same numbers? Run. 2. Ask: "How do I get the circle in the same place as the square?" (Add half the width and height to x and y. Processing can do the math: ellipse(100 + 125/2, 250 + 125/2, 125, 125).) 3. Ask: "What if I want to get rid of the circle?" You could delete it, or comment it out (next slide).
Six numbers: an (x, y) pair for each corner, in any order. 1. Add triangle(100, 400, 250, 100, 400, 400); and Run. Ask: "Which pair is the top corner?" (250, 100: the smallest y, because y counts down.) 2. Ask: "How would you flip it upside down?" (Swap which corner has the small y.) The house in the Variables deck uses a triangle for its roof. Reference practice: the reference has more shapes, like quad() (any four corners) and arc() (part of an ellipse; Pac-Man is an arc). Students can look them up when they need them. Picture: drawn to match the other grid diagrams. Source: Processing Reference, triangle(), https://processing.org/reference/triangle_.html (CC BY-NC-SA 4.0).
Answer: C. The rectangle's (60, 60) is its top-left corner, but the ellipse's (60, 60) is its center, so the circle is centered on the square's corner. The circle comes second, so it's on top: its white inside covers that corner of the square's outline. Each wrong answer is a common mistake: - A: placing the ellipse by its corner, like a rectangle. - B: placing the rectangle by its center, like an ellipse. - D: forgetting that later shapes are drawn on top. Ask students who picked a wrong answer to explain their choice; their reason tells you which rule to reteach. Then run the code to check. Pictures: drawn from the code (C) and from the mistake behind each wrong answer.
How screens make color, the color functions, transparency, and drawing order, then text(), the Processing reference, and the Taijitu activity. Source: "L04: Processing and Drawing," CSE 120, Winter 2020, University of Washington.
Switch partners again. We mix the brightness of red, green, and blue to get many different colors. Each brightness fits in a single byte (Intro to CS deck, Part 3): 0 is off, 255 is all the way on. The next two slides show those tiny lights up close.
About 2 minutes for both slides. This is a look, not something to memorize. Every screen uses red, green, and blue lights, but each kind of screen arranges them differently. They're so small and close together that your eye blends them into one color. That's why color(255, 255, 0) looks yellow on the next slide: the red and green lights are both on, and your eye can't see them separately. - CRT (cathode ray tube): the big, heavy screens most people used until the mid-2000s. Electron beams hit tiny dots or stripes of phosphor, which glow. A metal sheet behind the glass (the mask, or Sony's grille of thin wires) makes sure each beam hits only its own color. - Plasma: each subpixel is a tiny cell of gas. Electricity makes the gas give off ultraviolet light, which makes a coat of phosphor glow. - LCD (liquid crystal display): the subpixels don't make light. A white backlight shines through red, green, and blue filters, and the liquid crystals block or let through each one. - The OLPC XO-1 was the One Laptop per Child laptop (2007). Each pixel shows only one color. With the backlight off, the screen reflects sunlight and shows black and white, so it's easy to read outdoors. - Sharp's Quattron TVs (2010) added a real yellow subpixel. Ask: "Is there a yellow light in your screen?" (Almost certainly not. Yellow on most screens is red plus green.) If you have a magnifying glass, let students look at a monitor, at white and at yellow. Picture: simplified diagrams drawn from code for this deck, not photos. Real subpixel shapes, sizes, and spacing vary by model. The idea comes from the comparison picture in Wikipedia's "Pixel geometry" article.
- PenTile layouts don't give every pixel all three colors. In RGBG, every pixel gets a green (our eyes are most sensitive to green), but pixels share red and blue with their neighbors. RGBW adds a white subpixel to make the screen brighter. - OLED (organic LED): each subpixel makes its own light, so black is truly off. Most new phones use OLED. - LG's OLED TVs (WOLED) use white OLED light through color filters, plus a white subpixel with no filter for extra brightness. Newer LG panels (about 2024 on) put them in a different order (red, green, white, blue), and LG Display's newest monitor panel (announced December 2025) drops the white subpixel for a plain RGB stripe. - QD-OLED (Samsung Display, TVs and monitors from 2022) uses blue light and quantum dots that turn it into red and green. This is the original triangle layout. Samsung Display's newest panels (shipping since December 2025) use a "V-stripe" that looks much like a regular RGB stripe, for sharper text. - LED video walls (scoreboards, billboards) have a red, a green, and a blue LED in each little package. Stand close to one and you can see the dots. - MicroLED: every subpixel is its own microscopic LED, so most of the screen is black. Point to land: whatever the shape, every screen makes its colors from red, green, and blue light. That's why Processing describes colors with three numbers. Picture: simplified diagrams drawn from code for this deck, not photos. Sources: Samsung Display on V-stripe QD-OLED (https://global.samsungdisplay.com/31411); LG Display on its RGB-stripe OLED (https://news.lgdisplay.com/en/2025/12/lg-display-unveils-worlds-first-240hz-rgb-stripe-oled-panel/); FlatpanelsHD on QD-OLED V-stripe (https://www.flatpanelshd.com/news.php?subaction=showfull&id=1767260551); Tom's Hardware on V-stripe (https://www.tomshardware.com/monitors/gaming-monitors/samsung-display-finally-brings-v-stripe-subpixel-layout-to-qd-oled-mass-production-of-new-1-300-nit-34-ultrawide-panel-also-boosts-text-clarity); PC Monitors on QD-OLED and WOLED fringing (https://pcmonitors.info/articles/qd-oled-and-woled-fringing-issues/); TFTCentral on OLED RGB stripe (https://tftcentral.co.uk/articles/oled-rgb-stripe-panels-explained-should-you-wait); OLED-Info's macro photo of LG's WRGB subpixels (https://www.oled-info.com/lgs-wrgb-oled-tv-sub-pixels-captured-macro-photo).
Have pairs write down a guess for all eight colors first. Hint: think of mixing light, not paint. Red + green light = yellow. Then click to reveal the answers one at a time, asking for the class's guess before each click. The reveal works in the HTML version (GitHub Pages, or the .html file in a browser). VS Code's preview and PDF or PowerPoint exports show every answer from the start, so present this slide from the HTML version. Two details to keep: the answer lines start with * instead of - on purpose (that's how Marp makes them appear one per click), and the two columns come from the theme's list-cols class (the _class comment above the heading). The color squares are small pictures with a gray border, so white still shows. Students don't need to memorize this (they can look it up), but it's handy to understand. So how can we actually use colors?
1. Add background(0, 255, 255); at the *bottom* of the code and Run. Ask: "Oh no! What happened?" (The canvas was covered after the other shapes were drawn.) 2. Move background() to the top of the code and change it to background(255, 255, 255);.
Think: "What color have I dipped my paintbrush in?" 1. Add stroke(255, 0, 0); before the first line() and Run. 2. Ask: what if I want a thicker line? Add strokeWeight(5); and Run. 3. Ask: "What if we only want the diagonal line to be red?" Move stroke() below the first line, and add another stroke() before rect().
fill() only applies to shapes that have area. Add fill(255, 0, 255); before rect() and Run.
noStroke() takes no numbers, but it still needs its empty parentheses and semicolon. 1. After the rect, add noStroke(); and ellipse(350, 312, 125, 125); and Run. Ask first: "Will the square lose its outline too?" (No. noStroke() only changes shapes drawn after it, because Processing runs in order.) 2. Ask: "How would I give the next shape an outline again?" (Call stroke() again.) It's handy for shapes that shouldn't have an edge, like clouds, shadows, or the squares in pixel art. Its partner, noFill() (next slide), does the opposite: it turns off the inside and leaves only the outline. Picture: drawn from the code shown. Source: Processing Reference, noStroke(), https://processing.org/reference/noStroke_.html (CC BY-NC-SA 4.0).
noFill() is the partner of noStroke(): it turns off the inside instead of the outline. Like noStroke(), it takes no numbers but still needs its empty parentheses and semicolon. 1. Change the noStroke(); from the last slide to noFill(); and Run. Ask first: "What will the circle look like now?" (A green ring: the outline is still on, but there's no inside.) 2. Ask: "What happens if a shape has both noFill() and noStroke()?" (Nothing shows at all: there's nothing left to draw. Students who hit this think their shape vanished.) 3. Ask: "How would I fill the next shape again?" (Call fill() again.) Lines don't have an inside, so noFill() doesn't change them. It's handy for rings, outlines, and frames, like the O's in tic-tac-toe or the outline circle in the Taijitu activity. Picture: drawn from the code shown. Source: Processing Reference, noFill(), https://processing.org/reference/noFill_.html (CC BY-NC-SA 4.0).
Alpha is one more number from 0 to 255, like R, G, and B. 1. Type the code and Run. Ask: "Why is the middle purple?" (You can see the red circle through the blue one.) 2. Change both 150s to 255: the blue circle hides the red one again, because it's drawn later. Change them to 50: both are faint. Careful with the "mixing light" idea from the color slides: red and blue light add up to magenta, but see-through layers blend more like paint, so the overlap here is purple. Alpha works with stroke() too, and with grays: fill(0, 100) is see-through black (two numbers mean gray plus alpha). Game uses: shadows, ghosts, glass, and things fading in or out. Picture: drawn from the code shown. Sources: Daniel Shiffman, Learning Processing, 2nd ed., Chapter 1 (color transparency); Processing Reference, fill(), https://processing.org/reference/fill_.html (CC BY-NC-SA 4.0).
If all three values are the same, you just get shades of gray. A single number is a shortcut for the same value three times.
Demo the Color Selector. Point out the hex code at the bottom: #FFFFFF is (255, 255, 255). That's the hex connection from the Intro to CS deck (Part 3).
The rectangle demo already showed this (the rect covered the line until you swapped them), and background() is the extreme case: it covers everything drawn before it. Ask before revealing the picture: "If I draw a character's eyes before its head, what happens?" (The head covers them.) Students need this to plan a drawing: the U02A04 plan step has them number their shapes back to front, and its robot warm-up (question 6) moves the eyes above the head on purpose. Picture: drawn from the code shown, with Processing's default colors (gray background, white fill, black outline).
The answers appear one per click (the * lines; HTML version only). A bug like this doesn't make an error message: the program runs and draws exactly what the code says. That makes it harder to find than a missing semicolon, so read the code in order, like the computer does. Extension: "What if you moved only the face's ellipse() line above the eyes, and left its fill() where it is?" (The face would be drawn with fill(0), so it's black, and the black eyes would vanish into it. A shape's fill() has to move with it.) Picture: drawn from the code shown, at twice the size.
Ask: "What brush am I holding? What colors am I using right now?" If a shape comes out the wrong color, the culprit is usually a fill() or stroke() much earlier in the code.
Answer: A. Width 100 and height 200 make it tall; fill() is the inside (gold) and stroke() is the outline (maroon). Colors: Lindblom maroon and gold (the original UW deck used UW purple and gold).
Answer: B. The square is yellow. noFill() makes the circle a ring, which keeps its outline because the stroke is still on. noStroke() turns off outlines for everything after it, and fill(0, 150, 255) turns the inside color back on, so the bottom square and circle are solid blue with no outlines. Each wrong answer is a common mistake: - A: thinking fill() also brings back the outline (it doesn't; only stroke() does). - C: thinking noFill() didn't change the circle. - D: thinking noFill() is still on after fill(). With noStroke() too, the bottom shapes would be invisible. (A shape with both noFill() and noStroke() really does disappear.) Pictures: drawn from the code (B) and from the mistake behind each wrong answer.
Every game needs text: a score, lives, a title, "Game over." 1. Type the code and Run. 2. Ask: "What happens with text("Hi", 50, 0)?" (It's almost invisible: the words sit above y = 0, off the top of the canvas.) To be exact, y is the baseline, the line the letters sit on; letters like g and y hang below it. 3. Ask: "Why doesn't stroke() change the text color?" (Text uses fill(), like the inside of a shape.) Text in double quotes is called a String; the Variables deck adds String to the data types. Later, text("Score: " + score, 50, 100) shows a score variable, because + joins text and a number. Picture: drawn from the code shown (the font may look slightly different in Processing). Sources: Processing Reference, text(), https://processing.org/reference/text_.html, and textSize(), https://processing.org/reference/textSize_.html (CC BY-NC-SA 4.0).
There's a LOT in Processing that you can look up. It'll be more useful as the course goes on. Source: Processing Reference, https://processing.org/reference/ (CC BY-NC-SA 4.0).
We're going to make this shape in Processing using just 7 shapes. Let students find their own approach first. One approach: a big black circle; a white rectangle over its left half; a white medium circle on the top half and a black medium circle on the bottom half; two small dots in the opposite colors; and a final outline circle with noFill() and a black stroke. GP1 connection: this is decomposition, breaking a picture into shapes, which is the same skill as turning a board game's art into code.
Written for this class (not from CSE 120). The three kinds of bugs, reading error messages, and strategies that work without variables: commenting out lines, println() for messages and math, and more. The Variables and Functions decks each add their own debugging slides. Supports skill G114 (Testing, Debugging, and Playtesting).
Every programmer writes bugs; good programmers are good at finding them. Ask students for bugs they've already hit and sort them into the three kinds. - Syntax errors are the easiest to fix, because Processing points right at them. The sketch won't run at all until they're fixed. - Runtime errors happen while the sketch runs. Dividing a whole number by zero stops the sketch with ArithmeticException: / by zero. They're rare in what we've written so far. - Logic bugs are the hardest: the computer does exactly what the code says, which isn't what you meant. "Practice: Where did the eyes go?" in Part 4 was one. Where the word comes from: "bug" meant a flaw in a machine long before computers; Thomas Edison used it in the 1870s. The famous story is a 1947 logbook from the Harvard Mark II computer with a real moth taped to the page and the note "First actual case of bug being found." Grace Hopper, who worked on that computer, loved telling the story. The page is at the Smithsonian's National Museum of American History.
Error messages are clues, not insults. Read every word: "does not exist" almost always means a typo or a capital letter in a function's name, and "expects parameters like" means the wrong number (or kind) of values in the parentheses. Processing also checks as you type (Preferences → Continuously check for errors) and underlines problems in red, so you can often fix a bug before you press Run. The Errors tab, next to Console at the bottom of the window, lists each problem with its tab and line number. Why the line above? A missing ; isn't noticed until Processing reaches the next line and finds it doesn't make sense. Curly quotes: Google Docs turns straight quotes (") into curly ones (“ ”), so code copied from a Doc or a handout can have them. Processing's full message ends "Ctrl-T to autocorrect": Ctrl+T (Edit → Auto Format) changes them to straight quotes. The messages above are Processing 4's exact wording (from its English language file).
The answers appear one per click, so students can find each bug before you reveal it. That works in the HTML version (GitHub Pages, or the .html file in a browser); VS Code's preview and PDF show all three at once. The answer lines start with * on purpose; don't change them to -. Processing shows only one error at a time and stops at the first: here, Missing a semicolon “;” on line 1. Fix it and run again, and the next one appears: The function “Rect(int, int, int, int)” does not exist. Fix that, and the last one appears: The function “ellipse()” expects parameters like: “ellipse(float, float, float, float)” (float means a number that can have a decimal point; the Variables deck covers data types). Finding bugs by reading first saves a lot of run-fix-run. Line 4 is a common mistake: ellipse() always takes x, y, width, and height, so a circle still needs both a width and a height, and they're the same number.
// turns the rest of a line into a comment, so Processing skips it. That makes it a switch: turn a line off, run, and see whether the bug goes away. If it does, the line you turned off is involved. Demo: type the sketch without the // and run it. The circle is gone, with no error message: a logic bug. The white rectangle is invisible on the white background, which is what makes it sneaky. Comment out lines from the bottom up until the circle comes back. For a long sketch, turn off half of it at once. If the bug goes away, it's in that half; if not, it's in the other half. Keep halving, and you'll find the line in a few tries. (Turning off a line can cause new errors, like a missing size(), so turn off whole drawing steps, not random lines.) Select several lines and press Ctrl+/ to comment them all at once; press it again to uncomment.
println() ("print line") shows whatever is in its parentheses in the console: text in double quotes, or the answer to some math. It doesn't draw anything on the canvas. Leave a trail: a few println() messages show how far the sketch got, and in what order the lines ran. If a message never shows up, the sketch never got there. Check your math: Processing does multiplication and division before addition and subtraction, like math class, so 150 + 100 / 2 is 150 + 50 = 200. Printing a calculation is a quick way to check a coordinate before you use it, like the center of a shape for an eye. When you're done, delete the println() lines or comment them out. The Variables deck comes back to println() to watch a variable change while a sketch runs.
- Read it like the computer: this is what students did in Code Detective, and what "Practice: Find the bugs" asks for. Keep track of the current fill and stroke as you go. - One thing at a time: if you change three things and the bug goes away, you don't know which change fixed it, and one of the others may have made a new bug. (Same rule as changing one rule at a time when playtesting a board game.) - Rubber duck debugging: explaining your code line by line, even to a toy duck, often makes you notice the bug yourself. The name comes from The Pragmatic Programmer, by Andrew Hunt and David Thomas (1999). - Go back to what worked: this is why we commit after every change that works. In GitHub Desktop, right-click a file under Changes and choose Discard changes to go back to the last commit. - Ask for help: "It doesn't work" is hard to help with. "The circle should be green, but it's yellow, and I already checked the fill() line" is easy. It's the same thing a good GitHub issue says. Processing also has a Debugger (Debug → Enable Debugger). Set a breakpoint on a line (Debug → Toggle Breakpoint), run, then Debug → Step (Ctrl+J) runs one line at a time, and Debug → Show Variables shows each variable's value. It's worth a demo once students write longer sketches.
The answers appear one per click, so students can find each bug before you reveal it. That works in the HTML version (GitHub Pages, or the .html file in a browser); VS Code's preview and PDF show both at once. The answer lines start with * on purpose; don't change them to -. Have students say which strategy finds each bug. The syntax error: read the error message. The logic bug: Processing says nothing, so read it like the computer (keep track of the current fill) or comment out the last ellipse() to see which light is which. Follow-up: "Fix both bugs. Now how would you turn the light off, with only the outlines showing?" (noFill() before the three ellipses, plus stroke(255) so the outlines show on the dark background.)