For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to page URLs.
Skip to content

Unit 2: If Statements and Drawings

In this unit students make drawings that make decisions. Boolean expressions and if statements decide what to draw, and event handlers make the decisions happen when the user clicks or presses a key.

Teacher notes

  • Time: 10 class periods: 5 lessons (1 class each) and 5 classes for the unit project

  • AP Computer Science A: Boolean Expressions and if Statements (relational and logical operators, if/else if/else, comparing objects and Strings with .equals()).

  • Before this unit: students have finished Unit 1: Using Easy Draw Objects.

  • Event handlers are written as lambdas. Students only need the pattern event -> { ... }; the full idea of lambdas is not needed yet.

Learning Objectives

Students will be able to:

  1. Write boolean expressions with relational operators (<, >, <=, >=, ==, !=) and logical operators (&&, ||, !).

  2. Use if, if/else, and if/else if/else to decide what to draw.

  3. Compare String values with .equals() instead of ==.

  4. Write event handlers that use conditionals to respond to clicks and key presses.

  5. Use static variables to remember information between events.

Lesson 2.1: Drawing with Decisions

public class Main {
    public static void main(String[] args) {
        EasyDraw.loadCanvas();

        int hour = (int) (Math.random() * 24);

        if (hour >= 6 && hour < 18) {
            EasyDraw.setCanvasColor("skyblue");
            EasyDraw.Circle sun = new EasyDraw.Circle(500, 100, 60);
            sun.setColor("yellow");
        } else {
            EasyDraw.setCanvasColor("midnightblue");
            EasyDraw.Circle moon = new EasyDraw.Circle(500, 100, 50);
            moon.setColor("lightyellow");
        }

        EasyDraw.Text clock = new EasyDraw.Text(300, 550, "Hour: " + hour);
        clock.setSize(32);

        EasyDraw.end();
    }
}

Practice:

  1. Add an else if so the sky is "orange" at sunset (hours 18 and 19).

  2. Draw stars only at night.

Lesson 2.2: Click Events

An event handler is code that runs when something happens, like a click. The code goes inside event -> { ... }.

Discussion: Why do we use light.getColor().equals("gray") and not light.getColor() == "gray"? (== checks whether two references point to the same object. .equals() checks whether two strings have the same characters.)

Lesson 2.3: Remembering with Static Variables

A lambda cannot change a local variable from main. A static variable of the class can be changed, so it can remember information between clicks.

Practice: Add a Text object that shows "STOP", "GO", or "SLOW".

Lesson 2.4: Where Was the Click?

The event has the mouse position in event.x and event.y. Conditionals can check which part of the canvas was clicked.

Practice: Add a horizontal line so the four parts are easy to see. Then change the background color to match the part that was clicked.

Lesson 2.5: Keyboard Events and Boundaries

Keys move a player. Conditionals keep the player on the canvas.

Key presses only reach Easy Draw when the Easy Draw page is the active browser tab. Click the page first.

Practice:

  1. Turn the player red when it is in the right half of the canvas and blue in the left half.

  2. Draw a goal square. When the player is inside it, show "You win!". (Hint: use && to check both x and y.)

Unit Project: Choose Your Path

Students build an interactive drawing that changes based on what the user does. Ideas:

  • Mood Ring - clicking different parts of a face changes its expression and color.

  • Quiz Show - a question with clickable answer boxes that say "Correct!" or "Try again."

  • Maze Escape - arrow keys move a player who cannot leave the path and wins at the exit.

  • Weather Station - keys pick the weather and the scene changes to match.

Requirements:

  • At least one if/else if/else chain.

  • At least one compound boolean expression with && or ||.

  • At least one String comparison with .equals().

  • At least two event handlers, with at least one using event.x/event.y or event.keysym.

  • At least one static variable that remembers information between events.

Category
4 - Exceeds
3 - Meets
2 - Approaching
1 - Beginning

Conditionals

Nested and chained conditionals used purposefully

if/else if/else and compound expressions

Only simple if statements

No conditionals

Events

3+ handlers that work smoothly together

2+ handlers using event information

1 handler

Events do not work

State

Several static variables track the program's state

A static variable remembers information

State is attempted but buggy

No state

Code quality

Clear names, comments, and organization

Comments explain each handler

Few comments

Hard to read

Design

Creative, polished, with clear instructions

Works and is easy to use

Partly works

Does not work

Last updated

Was this helpful?