IAT 334 Lab 2 Computer Graphics: Rocket, PImage. June 4, 2010IAT 3342 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.

Slides:



Advertisements
Similar presentations
Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming.
Advertisements

A Quick Introduction to Processing
Variables and Operators
Emerging Platform#5: Processing 2 B. Ramamurthy 6/13/2014B. Ramamurthy, CS6511.
1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Programming Project 2 SORTING Lecture 05, file P2 Due January.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Text David Meredith Aalborg University.
Lesson Four: More of the Same
Three types, subtypes, and inheritance. The story up until now Everything in your computer is data Including programs Data is divided into objects Objects.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 26, 2013 Marie desJardins University.
Translate, Rotate, Matrix Pages Function Function Definition Calling a function Parameters Return type and return statement.
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.
Sep 26, Fall 2006IAT 800 Lecture 6 Methods and Classes.
DSA Processing. Links Processing.org My Processing page Ben Fry Ben Fry’s Thesis on Computational Information DesignThesis Casey Reas siteCasey Reas Casey.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
, Fall 2006IAT 800 Lab 2: Polygons, Transformations, and Arrays.
IAT 800 Lecture 4. Sept 18, Fall 2006IAT 8002 Outline  Programming concepts –Methods –Classes  Talk about project 1  Reading: Read Chapters 1-4 of.
Oct 17, Fall 2006IAT 800 ArrayList, Text, and more Image Manipulation.
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.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
IAT 800 Lecture 8. Oct 13, Fall 2006IAT 8002 Outline  Programming concepts –BImage BFont –Creating XImage –Typography.
IAT 355 Lecture 4 Computer Graphics: Rocket. May 9, 2014IAT 3552 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Classes / Objects An introduction to object-oriented programming.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
1 k Jarek Rossignac,  2008 Processing  Install Processing  Learn how to edit, run, save, export, post programs  Understand.
Java Spring PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.
Graphics Concepts CS 2302, Fall /3/20142 Drawing Paths.
Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Functions. Functions are named blocks of code. Functions allow complex programs to be broken down into smaller, simpler tasks. Functions allow commonly.
11 Making a Sprite Session 4.2. Session Overview  Describe the principle of a game sprite, and see how to create a sprite in an XNA game  Learn more.
Processing can load images, display them on the screen, and change their size, position, opacity, and tint. You can load gif, png and jpg images in processing.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Objects. The Heart of the Matter - The "Black Box" Object-oriented software is all about objects. An object is a "black box" which receives and sends.
Computation as an Expressive Medium Lab 2: Polygons, Transformations, and Arrays Evan.
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
LCC 6310 Computation as an Expressive Medium Lecture 2.
Words. Characters and Strings Character –A single character inside of single quotes char letter = 'A' ; char digit = '0' ; – Strings Zero or more character.
Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.
Computer Science I Recap: variables & functions. Images. Pseudo-random processing.
Processing TYWu. Where can I download? 2.0b9 Windows 32-bit.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 25, 2014 Carolyn Seaman Susan Martin.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Computation as an Expressive Medium Lab 2: Polygons, Transformations, and Arrays (Oh My) Micah.
IAT 265 Images in Processing PImage. Jun 27, 2014IAT 2652 Outline  Programming concepts –Classes –PImage –PFont.
Computer Science I Text input. Transformations. Yet another jigsaw. Classwork/Homework: Create new application making use of transformations.
IAT 800 Lecture 8 PImage and PFont. Oct 13, Fall 2009IAT 8002 Outline  Programming concepts –PImage –PFont.
Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.
Programming in Processing Taught by Ms. Madsen Assistants: Ms. Fischer and Ms. Yen Winsor School, 2/20/08.
Some of Chap 17.
Computer Graphics: Rocket, Java: Class
IAT 265 PImage and PFont.
Chapter 14, Translate & Rotate
Polygons, Transformations, and Arrays
LCC 6310 Computation as an Expressive Medium
IAT 265 Lecture 2: Java Loops, Arrays Processing setup, draw, mouse Shapes Transformations, Push/Pop.
Chap 17 Section 3.
LCC 6310 Computation as an Expressive Medium
Computation as an Expressive Medium
IAT 800 Foundations of Computational Art and Design
LCC 6310 Computation as an Expressive Medium
LCC 6310 Computation as an Expressive Medium
Presentation transcript:

IAT 334 Lab 2 Computer Graphics: Rocket, PImage

June 4, 2010IAT 3342 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods –Classes –PImage –PFont

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

Effect of creating an int variable May 21, 2010IAT 3344 // 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 21, 2010IAT 3345 // declare int array int[] intArray; // initialize int 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

June 4, 2010IAT 3346 Drawing a rocket background(0); fill(255); triangle(10, 0, 0, 20, 20, 20); rectMode(CORNERS); rect(5, 20, 8, 23); rect(12, 20, 15, 23);

June 4, 2010IAT 3347 Now I want to draw several rockets  Want several rockets in different locations on the screen  I could copy and paste the code –Need to adjust all the numbers for the new location  Or… define a method

June 4, 2010IAT 3348 First method for drawing a rocket void drawRocket() { fill(255); triangle(10, 0, 0, 20, 20, 20); rectMode(CORNERS); rect(5, 20, 8, 23); rect(12, 20, 15, 23); } Gotcha! Once you start using methods, all code must be in methods (can’t just directly call drawRocket() at the top of the file)

June 4, 2010IAT 3349 Didn’t seem to help much…  Still just draws a rocket at one fixed location  Need arguments that allow me to tell the program where I want the rocket! –Must figure out the relationship between the position and the location of the rest of the parts  Argument variables are available within the method, but not outside (method scope)

June 4, 2010IAT DrawRocket() with arguments void drawRocket(int x, int y, float rot) { final int halfHeight = 10; final int halfWidth = 10; triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3); } We’re purposely ignoring the arguments for now

June 4, 2010IAT Using pushMatrix() and popMatrix() void drawRocket(int x, int y, float rot) { final int halfHeight = 10; final int halfWidth = 10; pushMatrix(); translate(x, y); rotate(rot); triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3); popMatrix(); }

June 4, 2010IAT Classes

June 4, 2010IAT Classes  Java (Processing) is an object-oriented language  This means that parts of your program that you treat as conceptual things, become things (objects) in the program code  Objects are built from classes –Classes are the blueprint, objects are built from the blueprint –Objects are called instances of classes  Our rocket sure seems like a thing – let’s turn it into a class

June 4, 2010IAT Parts of a class  Classes define fields, constructors and methods  Fields are the variables that will appear inside every instance of the class –Each instance has it’s own values  Constructors are special methods that define how to build instances (generally, how to set the initial values of fields)  Methods are how you do things to instances

June 4, 2010IAT Defining the rocket class class Rocket { // fields float rotation = 0; float xPos; float yPos; final int halfWidth = 10; final int halfHeight= 10; // constructor Rocket( int initialX, int initialY, float initialRot ) { xPos = initialX; yPos = initialY; rotation = initialRot; }

June 4, 2010IAT Using the class to create instances  Classes define a type  You can now declare variables of this type and initialize them using the constructor  Like arrays, the keyword new is used to tell Java to create a new object (hmm, so arrays must be objects…) Rocket r1, r2 ; void setup() { r1 = new Rocket(75, 10, 0); r2 = new Rocket(50, 50, PI/2); }  Nice, but my rockets don’t do anything (need methods!)

June 4, 2010IAT Adding a draw routine to our Rocket void draw() { pushMatrix(); translate(xPos, yPos); rotate(rotation); triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3); popMatrix(); } Don’t need arguments because we use the fields But we could define additional arguments if we wanted to No Arguments!

June 4, 2010IAT Calling methods on objects  You call methods on instances  Think of the method as something your asking the object to do  For example, we can now ask the rockets to draw themselves r1.draw();  In general, to call a method, take the name of the variable holding the object + “.” + the method name myObject.myMethod();

June 4, 2010IAT What else do we want to do to the Rocket?  We may want to change the rotation rotateClockwise(); rotateCounterclockwise();  We may want to give the rocket a boost fireThrusters();

PImage June 4, 2010IAT 33420

June 4, 2010IAT Loading Images  Loading Images –Give your project a name and save. –Place the image file in: /sketchbook/ /data/ –Use this code: PImage im = loadImage(“ ”);

June 4, 2010IAT Displaying Images  image() shows your image. –image(im, 0, 0) will display your image from the last slide at the top left of the window.

June 4, 2010IAT Accessing Pixels  The PImage class allows you to access the RGB values of each individual pixel of the image, with the pixels[] array.  You can get the width and height of the image file using the width and height fields of PImage.

June 4, 2010IAT Accessing Pixels  How do we know which pixel to look for in the array?

June 4, 2010IAT Accessing Pixels  How do we know which pixel to look for in the array?

June 4, 2010IAT Accessing Pixels  How do we know which pixel to look for in the array?

June 4, 2010IAT Accessing Pixels  How do we know which pixel to look for in the array?

June 4, 2010IAT Accessing Pixels  Array Index –x + y*width (4, 0) = 4 + 0*5 = 4 (3, 2) = 3 + 2*5 = 13

June 4, 2010IAT Accessing Pixels  What would a piece of code look like that got a color from a pixel?  Let’s look at some applications of this. PImage im = loadImage(“test1.jpg”); color c1 = im.pixels[3 + 2*im.width]; // gets color at (3, 2) stroke(c1); // set our line color so we can draw with this color.

June 4, 2010IAT Window vs. Image  The drawing window also has a pixels[] array. –You must call loadPixels(); to get the image from the screen –You don’t need a PImage object. loadPixels(); color c2 = pixels[3 + 2*width]; // gives us the color at (3, 2) in the window.

June 4, 2010IAT Window vs. Image  When would we want to use both of these? –Use the Window’s pixels if you want to draw more things based on the image that’s already on the screen. –Use the Image’s pixels if you want to manipulate the pixels of the image before you draw them.

June 4, 2010IAT D Arrays  Java lets us make Arrays of Arrays, otherwise called 2D Arrays. These are very useful for accessing arrays of pixels like the ones we’ve been working with. int[][] bob = new int[3][4]; color[][] pixels2d = new color[200][200];

June 4, 2010IAT D Arrays  Processing doesn’t provide us with a 2D array of pixels to use, so let’s develop a class that will make manipulating pixels easier.

June 4, 2010IAT D Arrays  Interestingly, 2D Arrays are just covering up a 1D array much like the pixels[] array. color[][] pixels2d = new color[20][20]; color c2 = pixels2d[3][2]; color[] pixels1d = new color[400]; color c1 = pixels1d[3 + 2*20]; Underneath, these two pieces of code do the same thing. The 2D array convention just makes it easier for us to access the array based on things like our x and y values.

June 4, 2010IAT PFont  PFont is the Processing class for manipulating fonts –Like PImage for images  Use PFont with –PFont loadFont() – loads a font –textFont(PFont font, int size) – sets the current font –text(String str, int x, int y) – draws a string in the current font at the current location Also text(String str, float x, float y)

June 4, 2010IAT Simple example // the fonts must be located in the data directory PFont eureka = loadFont(" Eureka90.vlw "); PFont zig = loadFont(" Ziggurat-HTF-Black-32.vlw "); textFont(eureka, 44); text("word", 10, 30); textFont(zig, 44); text("word", 10, 60);

June 4, 2010IAT Use fill() to change the color of text // the fonts must be located in the data directory PFont eureka = loadFont(" Eureka90.vlw "); PFont zig = loadFont(" Ziggurat-HTF-Black-32.vlw "); fill( 0, 255, 0 ); textFont( eureka, 44); text( "word", 10, 30); textFont( zig, 44); fill( 255, 0, 0); text( "word", 10, 60);

June 4, 2010IAT textMode sets the alignment  textAlign( LEFT ); –x, y is the upper left hand corner of the text  textAlign( RIGHT ); –x, y is the upper right hand corner of the text  textAlign( CENTER ); –x, y is the upper center of the text

June 4, 2010IAT Producing text effects  All the transform methods apply to drawing text –That means you can translate, rotate, and scale text  Combined with draw(), this means you can move text around the screen in real time –Remember, the movement of the rocket and asteroids in our asteroids example was just translation and rotation  So you can make textual machines where text moves around the screen!

June 4, 2010IAT Processing example PImage im ; PFont eur ; PFont zig ; void setup() { size( 600, 600 ); im = loadImage( "cdshaw.96.jpg" ); for( int i = 600 ; i >= 0 ; i -= 50 ) image( im, i, i ); eur = loadFont( "Eureka90.vlw" ); zig = loadFont( "Ziggurat-HTF-Black- 32.vlw" ); textFont( eur ); } void draw( ) { image( im, mouseX-370, mouseY-210 ); color c1 = im.pixels[ * im.width ] ; loadPixels(); c1 = pixels[ * width ] ; stroke( c1 ); fill( c1 ); textAlign( LEFT ); ellipse( mouseX, mouseY, 20, 10 ); textFont( eur, 44 ); text( "Yo!", mouseX + 20, mouseY + 20 ); fill( 255, 0, 0); pushMatrix(); textAlign( RIGHT ); rotate( 0.2); textFont( zig, 44 ); text( "Yo?", mouseX, mouseY ); popMatrix(); }

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 21, 2010IAT 33441

Review  Graphics: triangle()draw a triangle pushMatrix() copy the top of the matrix stack translate()change XYZ location rotate()rotate about origin … draw in translated & rotated coordinates popMatrix()recover the previous matrix image( img, x, y ) June 4, 2010IAT 33442

Review  Object Oriented Programming –Class-- a type you define –Instance-- one variable of a class –Fields-- variables within a class –Methods-- functions that act within a class –Constructor-- create an instance of a class June 4, 2010IAT 33443

Review  Graphics/ OO Example –Rocket Translated & rotated to its new location Data of location stored within its class Each rocket has its own location –And its own data! –PImage is also a class. Each actual image is an object June 4, 2010IAT 33444