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.
Learning Objectives
Students will be able to:
Write boolean expressions with relational operators (
<,>,<=,>=,==,!=) and logical operators (&&,||,!).Use
if,if/else, andif/else if/elseto decide what to draw.Compare
Stringvalues with.equals()instead of==.Write event handlers that use conditionals to respond to clicks and key presses.
Use
staticvariables 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:
Add an
else ifso the sky is"orange"at sunset (hours 18 and 19).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.
Practice:
Turn the player red when it is in the right half of the canvas and blue in the left half.
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/elsechain.At least one compound boolean expression with
&&or||.At least one
Stringcomparison with.equals().At least two event handlers, with at least one using
event.x/event.yorevent.keysym.At least one
staticvariable that remembers information between events.
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?