IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Creative Computing. \\ aims By the end of the session you will be able to: 1.Move objects around 2.Write simple interactive programs 3.Use the mouse position.
Introduction to Macromedia Director 8.5 – Lingo
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Lesson One: The Beginning Chapter 3: Interaction Learning Processing: by Daniel Shiffman Presentation by Donald W. Smith Graphics from text.
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
Variables and Operators
Variables Conditionals Loops The concept of Iteration Two types of loops: While For When do we use them? Iteration in the context of computer graphics.
IAT 334 Java using Processing ______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY.
IAT 800 Foundations of Computational Art and Design Lecture 2.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
The switch Statement, DecimalFormat, and Introduction to Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
1 k Jarek Rossignac,  2008 Processing  Install Processing  Learn how to edit, run, save, export, post programs  Understand.
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
Review of ArT3 Programming Course David Meredith Aalborg University
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Lecture 02b: Tutorial for Programming in Processing Jarek Rossignac.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Lesson Two: Everything You Need to Know
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Mouse Inputs in Processing. Interacting with the Mouse mouseX and mouseY: pg mouseXmouseY –The position of the mouse in the canvas pmouseX and.
Lesson Two: Everything You Need to Know
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
Repetition 8/9/2009. Learning to Program -- Suggestions Spend a lot of time fiddling around with code –Programming is something you have to learn by doing.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
G RAPHICS & I NTERACTIVE P ROGRAMMING Lecture 2 More Programming with Processing.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
Review Expressions and operators Iteration – while-loop – for-loop.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
Welcome to Processing Who does not have access to digital camera?
1 SIC / CoC / Georgia Tech MAGIC Lab Rossignac Processing  Install Processing  Learn how to edit, run, save, export,
Programming for Art: Arrays – 2D ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 16 Fall 2010.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Variables. Something to mention… void setup(){ size(200, 200); background(255); smooth(); } void draw() { stroke(0); strokeWeight(abs(mouseX-pmouseX));
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Comp1004: Loops and Arrays I Whiles, For and Arrays[]
The switch Statement, and Introduction to Looping
Loop Structures.
Lesson Two: Everything You Need to Know
Computation as an Expressive Medium
Mouse Inputs in Processing
Chapter 5, Conditionals Brief Notes
More programming with "Processing"
February , 2009 CSE 113 B.
LCC 6310 Computation as an Expressive Medium
Loops & Nested Loops CSE 120 Winter 2019
Variables and Operators
Continuous & Random September 21, 2009.
Presentation transcript:

IAT 800 Lab 1: Loops, Animation, and Simple User Interaction

Sep 10, Fall 2009IAT 800 What have you gone over?  Window size and coordinate system  Commenting code  Variables  Drawing primitives –point –line –rect –ellipse –triangle  print() and println()  if(boolean){ do something }else{ do something} logic (0,0)x y

Sep 10, Fall 2009IAT 800 Today’s lab  Variables: A Recap  Drawing primitives (more info)  Java Control Flow: Looping  The Processing “draw()” looping function for animation  Simple mouse interaction

Sep 10, Fall 2009IAT 800 Variables  Variables are placeholders for your data. int i; // declares a new variable i i = 1; // assigns the value of 1 to the new variable int j = i + 1; // declares a new variable j, and assigns it to i + 1, which is 2.

Sep 10, Fall 2009IAT 800 Variables  Two main types: Primitives and Objects  Primitives: –Boolean, Char, Byte, Short, Int, Long, Float, Double  Objects: –Everything else. –Constructed with primitives as a base. –For example, a “String” object is a series of Char variables.

Sep 10, Fall 2009IAT 800 More info on drawing primitives  Reference pages… –

Sep 10, Fall 2009IAT 800 Java Control Flow  If-Then-Else: Conditional Logic int i = 5; if(i > 10) { draw_the_kitten(); } else if(i > 3) { erase_the_kitten(); } else { paint_the_kitten(); }

Sep 10, Fall 2009IAT 800 Java Control Flow: Looping  Draw ten lines, evenly-spaced apart line(10, 10, 10, 200); line(20, 10, 20, 200); line(30, 10, 30, 200);...  Tedious

Sep 10, Fall 2009IAT 800 Java Control Flow: Looping  Want to draw ten lines, evenly-spaced apart? line(10, 10, 10, 200); line(20, 10, 20, 200); line(30, 10, 30, 200);...  This can get old, fast. Also would be tedious if you wanted to change all of the lines to start 5 pixels higher, instead. What if we could run this same line of code multiple times, only changing these two numbers?

Sep 10, Fall 2009IAT 800 Java Control Flow: While Loops  Use a while loop line(10, 10, 10, 200); line(20, 10, 20, 200); line(30, 10, 30, 200);... int i = 10; while(i <= 100) { line(i, 10, i, 200); i = i + 10; }  A while loop will run the code inside the brackets repeatedly until the condition in the parentheses is false.

Sep 10, Fall 2009IAT 800 Java Control Flow: While Loops  Make sure the condition will eventually return false, or else it will go forever. while(true) {... } int i = 10; while(i > 0) { line(i, 10, i, 200); i = i + 10; }  Both of these loops are valid, will compile, and run infinitely.

Sep 10, Fall 2009IAT 800 Java Control Flow: For Loops  For Loops are just like While loops, but a bit more compact for simple incrementing  These two loops are functionally the same: int i = 10; while(i <= 100) { line(i, 10, i, 200); i += 10; } for(int i = 10; i <= 100; i += 10) { line(i, 10, i, 200); } ( i += 10 is equivalent to i = i + 10 )

Sep 10, Fall 2009IAT 800 Java Control Flow: Nested Loops  Nesting of loops refers to putting one loop inside the brackets of another. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); }  The inside loop will run through,  then i will increment,  then the inside loop will run again.

Sep 10, Fall 2009IAT 800 Java Control Flow: Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j Sets i to 10. First step.

Sep 10, Fall 2009IAT 800 Java Control Flow: Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j Sets j to 10. That’s all for now.

Sep 10, Fall 2009IAT 800 Java Control Flow: Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j i = 10, j = 10. Draws a point at (10, 10).

Sep 10, Fall 2009IAT 800 Java Control Flow: Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j Now we jump to the top of the innermost loop, and increment j by 10, and then check if it’s greater or equal to 100, which it’s not, so we continue.

Sep 10, Fall 2009IAT 800 Java Control Flow: Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j i is still 10, but j is now 20. We draw the new point, Then go to loop again.

Sep 10, Fall 2009IAT 800 Java Control Flow: Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j We keep looping in this j loop until it passes 100. Then what?

Sep 10, Fall 2009IAT 800 Java Control Flow: Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j Now that the program has exited from our j loop, It sees that it’s still inside our i loop, and increments i By 10.

Sep 10, Fall 2009IAT 800 Java Control Flow: Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j The program reaches the j loop again, so it loops All the way through drawing dots. Only this time, i is 20, so the dots are further to the right.

Sep 10, Fall 2009IAT 800 Java Control Flow: Nested Loops  Let’s look at this thing closer. for(int i = 10; i <= 100; i += 10) { for(int j = 10; j <= 100; j += 10) { point(i, j); } i j The i loop keeps incrementing in this way, drawing columns of dots, until i exceeds 100. The program then exits the code inside the i loop’s braces.

Sep 10, Fall 2009IAT 800 Java Control Flow: Nested Loops  Nested Loops are especially useful for drawing in grid formations in a 2-D space.  Think about how you could nest another loop inside to make a grid in a 3-D space.

Sep 10, Fall 2009IAT 800 The draw() function  What is the draw() function? –Processing allows you to put code in a special function called draw, which will run a number of times per second.  You need the draw() function: –to change graphics over time, –have the user interact with graphics

Sep 10, Fall 2009IAT 800 The draw() function  Simple example: float a = 200; void draw() { background(255); a = a - 1; if (a < 0) { a = height; } line(0, a, width, a); }

Sep 10, Fall 2009IAT 800 The draw() function  Simple example: float a = 200; void draw() { background(255); a = a - 1; if (a < 0) { a = height; } line(0, a, width, a); } Important! The variable declaration must be outside the loop function, or else it will be reset every time draw() is called. (Try putting it inside draw() to see what happens)

Sep 10, Fall 2009IAT 800 The draw() function  Simple example: float a = 200; void draw() { background(255); a = a - 1; if (a < 0) { a = height; } line(0, a, width, a); } For animation, this line is necessary To clear the last frame that was drawn.

Sep 10, Fall 2009IAT 800 The draw() function  Simple example: float a = 200; void draw() { background(255); a = a - 1; if (a < 0) { a = height; } line(0, a, width, a); } This is the meat of what makes the line move. Each loop, we subtract 1 from the variable a, which we later use when drawing the line, so it will appear to move. When a is 0, we’ve hit the top of the window, and reset a to the highest value.

Sep 10, Fall 2009IAT 800 The setup() function  You can use the setup() function to define some properties of your window once when your program first starts: –size(width, height): size of drawing window (in pixels) –framerate(fps): number of times per second that the draw() function is called. void setup() { size(640, 480); framerate(30); }

Sep 10, Fall 2009IAT 800 Tracking Mouse Position  Processing uses two global variables which have the current mouse position at all times: –mouseX, mouseY: current X and Y of cursor, relative to the top-left of drawing window. void draw() { background(204); line(mouseX, 20, mouseX, 80); } A simple program that moves a line left or right to follow your cursor. Note that it stops tracking your mouse when it’s not in the drawing window.

Sep 10, Fall 2009IAT 800 Reacting to Mouse Clicks  Use the variable mousePressed. void draw() { if(mousePressed) { point(mouseX, mouseY); } This simple function will draw a point in the window whenever the mouse is in the clicked position. Try clicking and dragging around the window and see what happens. Can you think of a way to make the drawing smoother?  There are similar variables for mouseReleased, mouseMoved, and mouseDragged.

Sep 10, Fall 2009IAT 800 That’s It For Today!  For and While Loops  Processing draw() function  Mouse Position and Clicking