## Session 5

### Opening Exercise

Last time we saw that we can create an RGB color object by:

new Color( red, green, blue )

Select the red, green, and blue values you think we'd need to make these colors:

You can see the values I used to make those colors.

### Where Were We?

A color is encoded as an ordered triple (red, green, blue). We can think of a color as a point in three-dimensional space:

One nice side effect of this way of thinking is that we have a natural way to decide how "close" two colors are to one another: distance in space!

A pixel consists of a color.

A picture consists of a two-dimensional grid of pixels.

### Pictures as Arrays of Pixels in Java

A sequence of similar objects can be modeled in Java using an array. An object's position is computed by its "distance from the front". So, like the red/green/blue components of a color, the position in an array is 0-based.

A picture is an array of arrays. The primary dimension is horizontal, and the secondary dimension is vertical. (This is similar to how we do coordinates in math, and unlike how we locate positions in many real-world applications. One difference from how we do coordinates in math: we are in "quadrant 4".)

... but the distinction won't matter to us oo much here at the beginning of our learning... For now, we will process all the pixels in an image, as if the pixels were in one long line.

### Interactions in Dr. Java

... to explore pictures, pixels, colors

... to distinguish class features (fields, methods) and object features (almost always methods)

... to do the same operation to many pixels

... to learn Java's shorthand for describing repeated steps: the for loop

• by counting through pixels by position
• by counting through all pixels
• by using a "for each" loop

Choosing a for-loop style: Use "for each" if you are processing every pixel in the image.

### Wrap Up

To re-create some of what we did today, use this annotated script of my Interactions pane, and the resulting versions of Picture.java. If it helps, you can follow along with my slides (PDF).

For next time, finish Chapter 4.

Start on Homework 1.

Eugene Wallingford ..... wallingf@cs.uni.edu ..... September 5, 2006