______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] | WWW.SIAT.SFU.CA.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Introduction to Programming
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
A Quick Introduction to Processing
Processing Lecture. 1 What is processing?
Variables and Operators
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 26, 2013 Marie desJardins University.
IAT 334 Lab 2 Computer Graphics: Rocket, PImage. June 4, 2010IAT 3342 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Processing Processing is a simple programming environment that was created to make it easier to develop visually oriented applications with an emphasis.
IAT 334 Java using Processing ______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY.
IAT 800 Foundations of Computational Art and Design Lecture 2.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
IAT 800 Foundations of Computational Art and Design ______________________________________________________________________________________ SCHOOL OF INTERACTIVE.
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.
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.
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
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.
chipKit Sense Switch & Control LED
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
LCC 6310 Computation as an Expressive Medium Lecture 1.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
CSCI 273: Processing An Introduction. Programming Languages –An abstract "human understandable" language for telling the computer what to do –The abstract.
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?
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.
CSCI Processing CSCI Introduction to Algorithm Design An Introduction.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Review of ArT3 Programming Course David Meredith Aalborg University
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Introduction to Processing. 2 What is processing? A simple programming environment that was created to make it easier to develop visually oriented applications.
CIS 3.5 Lecture 2.2 More programming with "Processing"
2-D Shapes, Color, and simple animation. 7 Basic Shapes Ellipse :: ellipse() Arc :: arc() Line :: line() Point :: point() Rectangle :: rect() Triangle.
Lesson Two: Everything You Need to Know
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
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.
LCC 6310 Computation as an Expressive Medium Lecture 2.
______________________________________________________________________________________ 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.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
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.
1 SIC / CoC / Georgia Tech MAGIC Lab Rossignac Processing  Install Processing  Learn how to edit, run, save, export,
Review Random numbers mouseX, mouseY setup() & draw() frameRate(), loop(), noLoop() Mouse and Keyboard interaction Arcs, curves, bézier curves, custom.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
Chapter 7 Continued Arrays & Strings. Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this,
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Variables. Something to mention… void setup(){ size(200, 200); background(255); smooth(); } void draw() { stroke(0); strokeWeight(abs(mouseX-pmouseX));
Introduction to Processing David Meredith Aalborg University Art &Technology, 3rd Semester Programming.
IAT 265 Multimedia Programming for Art and Design ______________________________________________________________________________________ SCHOOL OF INTERACTIVE.
LCC 6310 Computation as an Expressive Medium
Lesson One: The Beginning Chapter 1: Pixels Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from
Computation as an Expressive Medium
Chapter 10 Algorithms.
Mouse Inputs in Processing
More programming with "Processing"
IAT 265 Lecture 2: Java Loops, Arrays Processing setup, draw, mouse Shapes Transformations, Push/Pop.
LCC 6310 Computation as an Expressive Medium
Chapter 10 Algorithms.
IAT 800 Foundations of Computational Art and Design
LCC 6310 Computation as an Expressive Medium
LCC 6310 Computation as an Expressive Medium
Presentation transcript:

______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] | IAT 355 Java using Processing ______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |

Processing  Processing is built on top of Java  Supports script-like coding –Easy to get simple programs up fast –But allows transition to full Java programming  Has built-in methods and classes to make drawing easy  Easy to export program to applet May 7, 2014IAT 3552

Playing around  To learn how to program it is necessary to play around with code!!! –Don’t just wait for me to show you things  Processing makes this easy –Use the Reference in the Help menu –or –Play with the examples May 7, 2014IAT 3553

Saving your work  You should install Processing on your own machine –Do this immediately  Processing saves all projects in a directory you can select via preferences –You should always copy your code to your local disk May 7, 2014IAT 3554

Drawing in Processing  Automatic creation of display window  Window has a coordinate system for drawing May 7, 2014IAT 3555 x y

Let’s draw a point: point() point(x,y) – draws a point at the location x, y Let’s try it: point(50, 50) Unexpected token: null – what ??  Compiler errors appear in the bottom pane All lines must be terminated with a semicolon ; May 7, 2014IAT 3556

Drawing several points point( 30, 20 ); point( 85, 20 ); point( 85, 75 ); point( 30, 75 ); May 7, 2014IAT 3557

Comments  Comments are non-program text you put in the file to describe to others (and yourself) what you’re doing  Important for being able to look back at your code and understand it  Single-line comments begin with //  Multi-line comments begin with /* and end with */ Commenting and uncommenting lines is useful for figuring out code May 7, 2014IAT 3558

Drawing shapes: some primitives line(x1, y1, x2, y2); triangle(x1, y1, x2, y2, x3, y3); rect(x, y, width, height); rectMode() – CORNER, CORNERS, CENTER ellipse(x, y, width, height); ellipseMode() – CENTER, CENTER_RADIUS, CORNER, CORNERS Don’t use these draw modes!!! rectMode(), ellipseMode() May 7, 2014IAT 3559

Controlling color and line Colors represented as Red Green Blue (RGB) values Each one ranges from 0 to 255 Can also use Hue Saturation Value (HSV) space, but we won’t worry about this for now background(R,G,B); – set the background color stroke(R,G,B); – set the colors of the outline (default black) noStroke(); – no outline drawing around shapes fill(R,G,B); – set the fill color for shapes (default white) noFill(); – don’t fill the shapes (background shows through) strokeWeight(w); – line width for outlines (default 1) May 7, 2014IAT 35510

Drawing primitives revisited  What are the different syntax parts of a drawing primitive? line( 0, 0, 50, 50 ); May 7, 2014IAT method name parentheses contain arguments arguments, parameters

The drawing commands are methods  Methods are reusable commands –Like a little machine that does work for you –Let’s you reuse code without typing it over and over  The arguments tell the method precisely what to do  We’ll see later that you can define your own methods! May 7, 2014IAT 35512

Introduction to variables  A variable is a named box for storing a value  You can put values in a variable by using the assignment operator (aka “=“ ) e.g. x = 1;  To use the value stored in a variable, just use the variable’s name e.g. line(x, 0, 50, 50); May 7, 2014IAT 35513

Variables have a type  You must tell Processing (Java) what kinds of values can go in the box  You do this by giving a variable a type May 7, 2014IAT int x; // variable x can hold integers (int) int y; // variable y can hold integers (int) x = 20; // store 20 in x y = 30; // store 30 in y point(x, y); // use the values of x and y to draw a point

Effect of creating an int variable May 7, 2014IAT // Single int int anInt; // Put a value in the int anInt = 3; // Type error! anInt = “hello”; CodeEffect Name: anInt, Type: int 3 “hello” Can’t shove “hello” into an int

Assigned values must match the type May 7, 2014IAT int x; // variable x can hold integers (int) int y; // variable y can hold integers (int) x = 1.5; // store 1.5 in x causes an error!!! y = 30; // store 30 in y point(x, y); // use the values of x and y to draw a point

Why Types?  Tells system (and you) what kind of values to expect  System uses type to detect errors int pi = 3.14 ; //  error: 3.14 not an int May 7, 2014IAT 35517

The “primitive” types int – integers between 2,147,483,648 and 2,147,483,647 float – floating point numbers (e.g , -2.34) char – a single character (e.g. ‘c’) byte – integers between -128 and 127 boolean – holds the values true or false color – holds a color (red, green, blue, alpha) May 7, 2014IAT 35518

Can combine declaring and assigning  Declaring a variable means telling Processing its type int x;  Assigning a value to a variable means putting a value in the named box x = 1;  You can declare and assign at the same time int x = 1;  But only declare a variable once, otherwise you get an error May 7, 2014IAT 35519

Print and println  When working with variables, it is often convenient to look at their values  print() and println() print to the bottom processing pane –They do the same thing, except println starts a new line after printing May 7, 2014IAT 35520

Control flow  By default Processing (Java) executes the lines of a method one after the other –Sequential control flow –Unconditional – doesn’t matter what happens in the world  Often we want which steps are executed to depend on what else has happened  That is, we want conditional control flow –This is necessary in order to make anything that is interactive May 7, 2014IAT 35521

If if statements introduce conditional branches if ( ) { // do this code } have one of two values: true or false May 7, 2014IAT 35522

Some boolean expressions May 7, 2014IAT anInteger == 1 true if variable anInteger is equal to 1 x > 20 true if variable x is greater than 20 1 == 2 true if 1 is equal to 2, it’s not so this is false ! is the not operator – reverses true and false so, ! (1 == 2) is true This is not a boolean expression: int anInteger = 1;

Example Try changing the values of drawRect and drawAnX May 7, 2014IAT strokeWeight(2); // draw with heavier line stroke(200, 0, 0); // draw with red line boolean drawRect = true; boolean drawAnX = true; if (drawRect) { fill(0, 200, 0); // fill with green rect(30, 30, 40, 40); } if (drawAnX) { line(0, 0, 100, 100); line(100, 0, 0, 100); }

setup() setup() is a predefined Processing method that you define setup() is called once when a sketch first starts executing  Place any startup code in setup(), eg. –Setting the size –Setting the background color –Initializing variables… May 7, 2014IAT 35525

draw() draw() is a predefined Processing method that you define draw() is called repeatedly by the Processing system  Put code in draw() when you need to constantly update the display (for example, animating an object) May 7, 2014IAT 35526

Example of setup() and draw() int x; int y; void setup() { size(400, 400); background(0); x = 0; y = height/2; } void draw() { background(0); ellipse(x, y, 20, 20); x = x + 1; if (x > width) x = 0; } May 7, 2014IAT 35527

setup() and draw() are examples of callbacks  A callback function is defined by the programmer –The callback gets called in response to some internal event –You usually don’t call callback functions directly with your own code. –setup() and draw() are predefined within Processing as to-be-called-if-defined May 7, 2014IAT 35528

Controlling draw()  framerate() can be used to set the number of times per second that draw() is called –framerate(30) says to call draw() 30 times a second (if the computer is capable of it)  delay() delays execution for a certain number of milliseconds –delay(250) delays for 250 milliseconds (1/4 of a sec.) –You can use delay() or framerate() to determine how fast you want draw() to be called – framerate() is probably easier  noloop() tells the system to stop calling draw() –If you want to, for example, turn off animation  loop() tells the system to start calling draw() again –Use noloop() and loop() together to turn repeated drawing on and off May 7, 2014IAT 35529

Mouse variables  mouseX and mouseY – variables that automatically contain the current mouse location –pmouseX and pmouseY hold the previous location  mousePressed – boolean variable that is true if a mouse button is down –mouseButton – value is LEFT, RIGHT or CENTER depending on which button is held down May 7, 2014IAT 35530

Mouse callback methods  There are several built-in methods you can fill in to process mouse events mousePressed() mouseReleased() mouseMoved() mouseDragged() Example: void mousePressed() { if( mouseBotton == LEFT ){ println( “Left Mouse Button was pressed” ); loop(); // activate drawing again } May 7, 2014IAT 35531

Loops  Sometimes you want to execute code multiple times –E.g. draw() is being called in a loop  Java provides a number of looping mechanisms  They all test some boolean expression (just like an if statement does) and continue to execute code while the expression is true May 7, 2014IAT 35532

while loops while( ) { }  Repeatedly executes the code body while the boolean expression is true May 7, 2014IAT 35533

for loops for( ; ; ) { } First executes the initialization statement  Then tests the boolean expression – if it's true, executes the code once  Then repeats the following: –execute final statement, –test boolean expression  execute code if true May 7, 2014IAT 35534

Converting for to while  Seeing how for loops can be converted to while loops helps you understand for for( ; ; ) { } // is the same as ; while( ) { ; } May 7, 2014IAT 35535

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) float milliseconds() – returns number of millis since start of app May 7, 2014IAT 35536

draw() has nothing to do with time  The value returned by second() or milliseconds() has nothing to do with how often draw() is called  In draw() you draw frames – you don’t know how often it will be called  Put a println in loop to see how often it gets called long lastTimeLoopWasCalled = 0; void draw() { long milliseconds = millis(); println(milliseconds - lastTimeLoopWasCalled); lastTimeLoopWasCalled = milliseconds ; } May 7, 2014IAT 35537

Arrays  An array is a contiguous collection of data items of one type  Allows you to structure data –Accessed by index number May 7, 2014IAT 35538

Effect of creating an int variable May 7, 2014IAT // Single int int anInt; // Put a value in the int anInt = 3; // Type error! anInt = “hello”; CodeEffect Name: anInt, Type: int 3 “hello” Can’t shove “hello” into an int

Creating an array of ints May 7, 2014IAT // declare byte array int[] intArray; // initialize byte array intArray = new int[5]; // set first element intArray[0] = 3; // set third element intArray[2] = 5; CodeEffect Name: intArray, Type: int[] each element has type int

Practice reading code  If code is a medium, then it can be both written and read  Reading code reveals –New programming constructs –Strategies and techniques (design patterns) –Style –Philosophical assumptions (deep reading) May 7, 2014IAT 35541