Class 16 — Wednesday, October 21

Progress Exam 2’s routine once through, in the exam seat. Then graphics: A1’s flags again, in Java, with Img.

A7 first, on the projector. Then the exam seat, once for practice. Then graphics: the flags of the first week, in Java.

A7 was due last night. A8 goes out today and is due Tuesday, October 27. Progress Exam 2 — Java, Monday, October 26, in a seat like today’s. The reading for Monday is [CU] 3.7 — Graphics, [CU] 4.1 — Design Principles for Modular Programs and [CU] 4.2 — Helper Methods in Practice.

Outline

  1. The exam seat, once for practice — on your own
  2. Get the graphics project — before the break
  3. A flag is a picture with a size — predict, then run
  4. Flags — pairs, at the keyboard
  5. The reading’s way — keyboards down
  6. Keep going — two more
  7. Before Monday

1. The exam seat, once for practice

On your own. This is Progress Exam 2’s routine, on two problems that count for nothing.

  1. Open Brightspace in LockDown Browser and start the quiz Class 16 practice.
  2. In the quiz’s header, click open my seat. Your seat opens: VS Code, in the browser, with its README first.
  3. The README says what is in the seat. Each problem is the comment at the top of its file, Problem1.java and Problem2.java, and Run, just above void main(), runs that file’s checks.
  4. The header’s second link, submit my files, sends your files to your repository on GitHub. Press it whenever you like; the last one counts.
  5. When you are done, type done in the quiz’s one box and submit the quiz. That is what closes LockDown Browser.

If a page says something you did not expect, put your hand up. That is what today is for.

2. Get the graphics project

Before the break, so that IDEA fetches what it needs while you are away.

This project WON’T WORK in online compilers like onecompiler.com.

In IDEA, from the project you have open: the menu button (☰, top left) → File → New → Project from Version Control… → paste

https://github.com/cu-cs-labs/cmsc-120-graphics

→ Clone.

Then watch the bottom right corner of IDEA: a popup asks to Load Maven plugins. Confirm it, and wait while IDEA fetches them.

Project’s README says what each file does. Importantly, the Img.java file implements Pyret’s images for Java: it providesImg.rectangle, Img.beside, Img.show and the rest, listed in the comment at its top. Keep it beside your files and do not edit it.

3. A flag is a picture with a size

Write your prediction in the Journal before anyone runs anything.

Open Tricolor.java and run it: a window opens with the flag in it, and the program ends when you close it. JavaFX prints a few lines starting with WARNING: in red as it starts. They are about JavaFX, not about your code.

Windows may ask for a permission when the program first runs. Deny it: the program works anyway.

Now, below tricolor(), three more pictures. What are the width and the height of each?

Img.beside(tricolor(), tricolor())
Img.above(tricolor(), Img.rectangle(90, 10, "solid", "black"))
Img.overlay(Img.circle(20, "solid", "gold"), tricolor())

180 by 60, 90 by 70, and 90 by 60. beside adds the widths and keeps the taller height; above adds the heights and keeps the wider width; overlay adds nothing, and keeps the larger of each. Pyret’s three did the same, which is why a flag’s size can be worked out on paper before it is drawn.

4. Flags

Pairs protocol
  1. The navigator (not typing) says the examples first. Then they say the steps the function takes.
  2. The driver types the above, and decides how each step becomes Java.
  3. If either of you is stuck, drop the roles for a minute and talk it over.
  4. Swap at every function.

What you write with your partner in class may go in both your files.

A new file in src/, Flags.java, with a main that ends in Check.done() and then shows each flag: Img.show(poland(20));.

Each function gets a // line above it with a purpose statement and at least two Check.is lines in main, on Img.width and Img.height, at two different units. Every size in a flag comes from unit.

poland

Img poland(int unit) — white above red, each band 4 * unit wide and unit high. poland(20) is 80 by 40.

Done when its checks pass at two units and the flag shows.

armenia

Img armenia(int unit) — three bands, red, blue and orange, each 4 * unit wide and unit high. above takes exactly two pictures, as in Pyret.

Done when its checks pass at two units and the flag shows.

bangladesh

If you are done, here are two more.

Img bangladesh(int unit) — dark green, 10 * unit by 6 * unit, with a red disc of radius 2 * unit in the middle. The real disc sits a little to the left; overlay puts it in the middle, and that is close enough today.

Done when its checks pass at two units and the flag shows.

switzerland

Img switzerland(int unit) — a red square, 5 * unit on a side, with a white cross: two white bars, 3 * unit long and unit thick, one across and one up.

Done when its checks pass at two units and the flag shows.

5. The reading’s way

Keyboards stop.

Reading.java runs the reading’s own program, RandomTricolorFlag from [CU] 3.7 — Graphics, as the book prints it. Run it, then open it: it is in src/chap03/sect7/.

The book puts each Rectangle at an x and a y. Where, in its program, is beside?

In the x of the second and the third stripe: new Rectangle(stripeWidth, 0, ...) and new Rectangle(2 * stripeWidth, 0, ...). The book works out where each stripe starts; Img.beside works it out for you, from the widths.

The two ways meet. The book’s shapes go straight into Img.beside, above and overlay, and every Img shape takes the book’s Color as well as a color’s name:

import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

// a stripe made the reading's way: unit wide, 2 * unit high, filled with c
Rectangle stripe(Color c, int unit) {
    return new Rectangle(unit, 2 * unit, c);
}

Img.beside(stripe(Color.RED, 30), stripe(Color.BLUE, 30)) is 60 by 60. The book’s x and y do not matter here: beside decides where each goes.

The two import lines go at the very top of Flags.java.

6. Keep going

shades

Img shades(Color a, Color b, int unit) — five stripes side by side, each made with stripe, from a to b: the second a quarter of the way from a to b, a.interpolate(b, 0.25), the third halfway, the fourth three quarters. shades(Color.RED, Color.BLUE, 20) is 100 by 40.

Done when its checks pass and the five stripes show, red to blue.

bordered

Img bordered(Img i, String color) — the picture i with a border 5 pixels wide all round it, in that color. It takes any picture, so its size comes from Img.width(i) and Img.height(i). bordered(poland(20), "black") is 90 by 50.

Done when checks pass on two different flags, and a bordered flag shows.

7. Before Monday

Progress Exam 2 — Java, Monday, October 26, in the first half of class, in an exam seat like today’s: its page says what it covers. A8 is due Tuesday, October 27.