LCC 6310 The Computer As An Expressive Medium
LCC 6310 The Computer As An Expressive Medium
The Computer as an
Expressive Medium
Lecture 3
Suggestions on learning to program
Spend a lot of time fiddling around with code
Programming is something you have to learn by trying it
noStroke();
fill(yellowish);
rect(x, y, squareSide, squareSide);
This enables writing custom methods and classes and using keyboard
and mouse events… more on this later!
Put code in draw() when you need to constantly update the display
(for example, animating an object)
Example of setup() and draw()
int x, y; // declare integer variables x and y
void draw() {
background(0);
ellipse(x, y, 20, 20);
x = x + 1;
if (x > width) { // width is a system variable, see reference
x = 0;
}
}
void draw() {
background(255);
pos = pos + 1;
line(pos, 0, pos, 100);
if (pos > width) {
pos = 0;
}
}
delay()
int pos = 0;
void draw() {
background(255);
pos = pos + 1;
line(pos, 0, pos, 100);
if (pos > width) {
pos = 0;
}
delay(250); // stops the program for 250 milliseconds
// try out different delay values
}
noLoop()
boolean looping = true; // try changing this to false!
int i = 0;
void setup() {
if (!looping) {
noLoop();
}
}
void draw() {
println("count "+i);
i = i + 1;
}
Getting info from the mouse
mouseX and mouseY – variables that automagically contain the
current mouse location
pmouseX and pmouseY hold the previous location
void draw() {
fill(value);
rect(25, 25, 50, 50);
}
void mouseMoved() {
if (goingUp){
value += 5;
if (value >= 255)
goingUp = false;
}
else{
value -= 5;
if (value <= 0)
goingUp = true;
}
}
Or……..
int value = 0;
boolean goingUp = true;
void draw() {
fill(value);
rect(25, 25, 50, 50);
}
void mouseMoved() {
value = (goingUp) ? (value + 5) : (value - 5); // what the !??!!
if (value > 255) {
goingUp = false;
} else if (value <= 0) {
goingUp = true;
}
}
Loops
Sometimes you want to execute code multiple times
E.g. draw() is being called in a loop
They all test some boolean expression (just like an if statement does)
and continue to execute code while the expression is true
while loops
while(<boolean exp>) {
<code to execute multiple times>
}
Executes the code within the curly brackets while the boolean
expression is true
for loops
for(<init statement>; <boolean exp>; <final statement>) {
<code to execute in loop>
}
is the same as
<init statement>
while(<boolean exp>) {
<code>
<final statement>
}
Reading time
int hour() – returns the hour (0 – 23)
int minute() – returns the minutes (0 – 59)
int second() – returns the seconds (0 – 59)
int day() – returns the day of the month (1 -31)
int month() – returns the month (1 – 12)
int year() – returns the four digit year (e.g. 2004)
long millis() – returns number of millis since start of app
long lastTimeLoopWasCalled = 0;
void draw() {
long milliseconds = millis();
println(milliseconds - lastTimeLoopWasCalled);
lastTimeLoopWasCalled = milliseconds;
}
Arrays
Hold a series of data elements of the same type
One of the simplest data structures, think of it as a convenient way to
store a whole lot of elements of the same type
You can then access any particular element if you know at which
position in the array it is located, i.e. its index
Code Effect
Code Effect
0 1 2 3 4
// initialize int array 0 0 0 0 0
intArray = new int[5];
each element has type int
// set first element 0 1 2 3 4
intArray[0] = 3; 3 0 0 0 0
Type casting
float val = (float)10/3;
print(val);
Assignment 1
Posted online, due Friday 5pm
A1-01: Draw three lines.
A1-02: Draw five lines.
A1-03: Draw three ellipses.
A1-04: Control the position of two lines with one variable.
A1-05: Control the position and size of two lines with two variables.
A1-06: Control the properties of two shapes with two variables.
A1-07: Create a simple, regular pattern with six lines.
A1-08: Program your pattern from Assignment 1-07 using while().
A1-09: Draw a layered form with two new loops.
A1-10: Redo Assignment 1-05 using mouseX and mouseY as the variables.
A1-11: Draw two visual elements that each move in relation to the mouse in a different way.
A1-12: Draw three visual elements that each move in relation to the mouse in a different way.
A1-13: Move a visual element across the screen. When it disappears off the edge, move it
back into the frame.
A1-14: Draw a visual element that moves in relation to the mouse, but with a different
relation when the mouse is pressed.
A1-15: Using if and else, make the mouse perform different actions when in different parts of
the window.
A1-16: Develop a kinetic image which responds to the mouse.
Remember…
For Thursday this week: Theory Readings
Two presenters (you know who you are!)
Everyone else: prepare one discussion question for each reading
Man-Computer Symbiosis - J.C.R. Licklider (NMR p.73)
Personal Dynamic Media - Alan Kay & Adele Goldberg (NMR p.391)