IAT 265 Java Loops Arrays Processing setup draw
- Slides: 25
IAT 265 Java Loops, Arrays Processing setup, draw, mouse Lecture 2
Suggestions on learning to program g Spend a lot of time fiddling around with code – Programming is something you have to learn by doing it g Get help from other people – I expect those who already know some programming to help others – Figure things out in groups g Ask me questions in class May 12, 2016 IAT 265 2
Agenda g Processing – setup(), draw(), mouse. X, mouse. Pressed… g Java – if / else – Loops – Arrays May 12, 2016 IAT 265 3
setup() Syntax void setup() is a predefined Processing method that you define setup() is called once when a sketch first starts executing Semantics g Place any startup code in setup(), eg. – Setting the size – Setting the background color – Initializing variables… May 12, 2016 IAT 265 Design 4
draw() is a predefined Processing method that you define draw() is called repeatedly by the Processing system g Put code in draw() when you need to constantly update the display (for example, animating an object) May 12, 2016 IAT 265 5
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); x = x + 1; if (x > width) x = 0; } May 12, 2016 IAT 265 6
setup() and draw() are examples of callbacks g. A callback function is defined by the programmer – The callback gets called in response to some internal event – You usually don’t callback functions directly with your own code. – setup() and draw() are predefined within Processing as to-be-called-if-defined May 12, 2016 IAT 265 7
Controlling draw() g frame. Rate() can be used to set the number of times per second that draw() is called – frame. Rate(30) says to call draw() 30 times a second (if the computer is capable) g 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 frame. Rate() to determine how fast you want draw() to be called – frame. Rate() is probably easier g no. Loop() tells the system to stop calling draw() – If you want to, for example, turn off animation g loop() tells the system to start calling draw() again – Use no. Loop() and loop() together to turn repeated drawing on and off May 12, 2016 IAT 265 8
Mouse variables g mouse. X and mouse. Y – variables that automatically contain the current mouse location – pmouse. X and pmouse. Y hold the previous location g mouse. Pressed – boolean variable that is true if a mouse button is down – mouse. Button – value is LEFT, RIGHT or CENTER depending on which button is held down May 12, 2016 IAT 265 9
Mouse callback methods g There are several built-in methods you can fill in to respond to mouse events mouse. Pressed() mouse. Moved() mouse. Released() mouse. Dragged() Example: void mouse. Pressed() { if( mouse. Button == LEFT ){ println( “Left Mouse Button was pressed” ); loop(); // activate drawing again } } May 12, 2016 IAT 265 10
If if statements introduce conditional execution if ( <boolean expression> ) { // do this code } <boolean expressions> have one of two values: true or false May 12, 2016 IAT 265 11
Example boolean draw. Rect = true; boolean draw. An. X = true; if (draw. Rect) { fill( 0, 200, 0 ); // fill with green rect( 30, 40, 40 ); } if (draw. An. X) { line( 0, 0, 100 ); line( 100, 0, 0, 100 ); } Try changing the values of draw. Rect and draw. An. X May 12, 2016 IAT 265 12
Example if / else boolean draw. Rect = true; boolean draw. An. X = true; if (draw. Rect) { fill( 0, 200, 0 ); // fill with green rect( 30, 40, 40 ); } else { fill( 0, 200, 220 ); // fill with cyan ellipse( 30, 40, 40); } if (draw. An. X) { line( 0, 0, 100 ); line( 100, 0, 0, 100 ); } May 12, 2016 IAT 265 13
Nested if boolean draw. Rect = true; boolean draw. Ellipse = true, draw. Triangle = true ; if (draw. Rect) { fill( 0, 200, 0 ); // fill with green rect( 30, 40, 40 ); } else if( draw. Ellipse ) { fill( 0, 200, 220 ); // fill with cyan ellipse( 30, 40, 40); } else if( draw. Triangle ) { triangle( 30, 30, 80, 30 ); } May 12, 2016 IAT 265 14
Loops g Sometimes you want to execute code multiple times – E. g. draw() is being called in a loop g Java provides a number of looping mechanisms g They all test some boolean expression (just like an if statement does) and continue to execute code while the expression is true May 12, 2016 IAT 265 15
while loops while( <boolean exp. > ) { <code to execute each time> } g Repeatedly executes the code body while the boolean expression is true May 12, 2016 IAT 265 16
for loops for( <init. statement> ; <boolean expression> ; <final statement> ) { <code to execute each time in loop> } First executes the initialization statement g Then tests the boolean expression – if true, executes the code once g Then repeats the following: – execute final statement, – test boolean expression execute May 12, 2016 IAT 265 code if true 17
Converting for to while Seeing how for loops can be converted to while loops helps you understand for(<init. stmt> ; <boolean exp> ; <final stmt> ) { g <code> } // is the same as <init. stmt> ; while( <boolean exp> ) { <code> <final stmt> ; } May 12, 2016 IAT 265 18
Reading time in Processing 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 12, 2016 IAT 265 19
draw() has nothing to do with time g g g 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 last. Time. Loop. Was. Called = 0; void draw() { long milliseconds = millis(); println(milliseconds - last. Time. Loop. Was. Called); last. Time. Loop. Was. Called = milliseconds ; } May 12, 2016 IAT 265 20
Arrays g An array is a contiguous collection of data items of one type g Allows you to structure data – Accessed by index number May 12, 2016 IAT 265 21
Effect of creating an int variable Code Effect Name: an. Int, Type: int // Single int an. Int; // Put a value in the int an. Int = 3; Name: an. Int, Type: int 3 // Type error! an. Int = “hello”; Name: an. Int, Type: int “hello” Can’t shove “hello” into an int May 12, 2016 IAT 265 22
Creating an array of ints Code Effect Name: int. Array, Type: int[] // declare int array int[] int. Array; // initialize int array int. Array = new int[5]; // set first element int. Array[0] = 3; 1 2 3 4 0 0 0 each element has type int // set third element int. Array[2] = 5; May 12, 2016 0 IAT 265 0 1 2 3 4 3 0 0 0 1 2 3 4 3 0 5 0 0 23
Practice reading code g If code is a medium, then it can be both written and g Reading code reveals read – New programming constructs – Strategies and techniques (design patterns) – Style May 12, 2016 IAT 265 24
Practice reading code g If code is a medium, then it can be both written and g Reading code reveals read – – New programming constructs Strategies and techniques (design patterns) Style Philosophical assumptions (deep reading) May 12, 2016 IAT 265 25
- Iat 265
- Iat 265
- Iat 265
- Java pimage
- Rocket java
- Parallel arrays in data structure
- Variables unidimensionales ejemplos
- Arreglos bidimensionales java
- Structured text array
- Nested loops java
- Lirik pkj 265
- Secur id
- Fe'llarning ma'no turlari
- Revenue cycle internal controls
- Array of arrays c++
- Parallel arrays
- Partially filled arrays
- C++ parallel arrays
- Why do we need arrays?
- Dynamic arrays and amortized analysis
- Arrays in mips
- Polynomial representation using array in c
- Arrays in arm assembly
- Global arrays in c
- Computer science arrays
- Searching and sorting arrays in c++