Intro to Java & Processing
Review CS is about problem-solving CS is about problem-solving To write programs you must To write programs you must –Be able to solve the problem yourself –Be able to describe the solution to another –Be able to describe the solution to the computer (programming)
Programming Programs: how to tell a computer what to do Programs: how to tell a computer what to do –Each line is a command to the computer –The computer is stupid, so you have to type everything in exactly –If you make a mistake, you get an error and have to fix it In this class, we use a programming language called Java In this class, we use a programming language called Java –Java is called an Object-Oriented language
About Programming Java is Case-SENsITivE! Java is Case-SENsITivE! Punctuation matters Punctuation matters Spelling matters Spelling matters File names matter File names matter Spaces don’t matter much Spaces don’t matter much Be careful! Be careful! Follow the examples… Follow the examples…
The tool… IDE: Integrated Development Environment IDE: Integrated Development Environment A Program that does it all together, and it’s PRETTY… A Program that does it all together, and it’s PRETTY… One IDE is Eclipse One IDE is Eclipse
Fun with Graphics We use a Java library called Processing that makes graphics easy. We use a Java library called Processing that makes graphics easy. Just open a new Processing project and check out the src folder. Just open a new Processing project and check out the src folder. Pixels! Pixels! –Color (RGB, 0-255, Additive model, think LIGHT!) –x,y value
Computer Graphics
How it works 2 sections: setup & draw 2 sections: setup & draw Setup: stuff that happens when the window opens (once) Setup: stuff that happens when the window opens (once) Draw: stuff that happens many times a second Draw: stuff that happens many times a second
Processing commands background(255,0,0); background(255,0,0); size(800,450); size(800,450);
Shapes triangle(Ax,Ay,Bx,By,Cx,Cy); triangle(Ax,Ay,Bx,By,Cx,Cy); rect(ULx, ULy, w, h); rect(ULx, ULy, w, h); ellipse(Cx,Cy,w,h); ellipse(Cx,Cy,w,h); What’s the difference in putting these in setup() vs draw()? Try it. What’s the difference in putting these in setup() vs draw()? Try it. What’s the difference between drawing a rectangle then a triangle or the triangle then a rectangle? Try it. What’s the difference between drawing a rectangle then a triangle or the triangle then a rectangle? Try it.
Circles & Arcs Angles done in radians & clockwise Angles done in radians & clockwise You must put the smaller number before the larger number. Add TWO_PI if you need to to get a bigger number. You must put the smaller number before the larger number. Add TWO_PI if you need to to get a bigger number.
Shape Attributes fill(R,G,B); fill(R,G,B); stroke(R,G,B); stroke(R,G,B); noFill(); noFill(); noStroke(); noStroke(); strokeWeight(n); strokeWeight(n);
Comments Don’t do too much without comments! Don’t do too much without comments! // - one line comment // - one line comment /* /* –paragraph */
ERROR!! Sometimes it just doesn’t work… Sometimes it just doesn’t work… Types of errors: Types of errors: –Compiler error No semicolon No semicolon public Class Program{ public Class Program{ –Logic error answer = 2+3; answer = 2+3; “The anser is “ “The anser is “ –Fatal or Runtime error answer = 2/0; answer = 2/0;
And then what? Fix the program, save it, recompile, re- execute until everything works Fix the program, save it, recompile, re- execute until everything works The error is often called a “bug” The error is often called a “bug” Fixing it is called debugging Fixing it is called debugging
There’s lots more… You get a cheat sheet and the API! You get a cheat sheet and the API!
PW0 Make a face Make a face Turn it in Turn it in