SlideShare a Scribd company logo
1 of 44
IAT 334
Lab 2
Computer Graphics: Rocket,
PImage
June 4, 2010 IAT 334 2
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, 2010 IAT 334 3
Effect of creating an int
variable
May 21, 2010 IAT 334 4
// Single int
int anInt;
// Put a value in the int
anInt = 3;
// Type error!
anInt = “hello”;
Code Effect
Name: anInt, Type: int
3
Name: anInt, Type: int
Name: anInt, Type: int
“hello”
Can’t shove “hello” into an int
Creating an array of ints
May 21, 2010 IAT 334 5
// declare int array
int[] intArray;
// initialize int array
intArray = new int[5];
// set first element
intArray[0] = 3;
// set third element
intArray[2] = 5;
Code Effect
Name: intArray, Type: int[]
0 1 2 3 4
each element has type int
0 0 0 0 0
0 1 2 3 4
3 0 0 0 0
0 1 2 3 4
3 0 5 0 0
June 4, 2010 IAT 334 6
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, 2010 IAT 334 7
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, 2010 IAT 334 8
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, 2010 IAT 334 9
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, 2010 IAT 334 10
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, 2010 IAT 334 11
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, 2010 IAT 334 12
Classes
June 4, 2010 IAT 334 13
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, 2010 IAT 334 14
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, 2010 IAT 334 15
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, 2010 IAT 334 16
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, 2010 IAT 334 17
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, 2010 IAT 334 18
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, 2010 IAT 334 19
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, 2010 IAT 334 20
June 4, 2010 IAT 334 21
Loading Images
 Loading Images
– Give your project a name and save.
– Place the image file in:
• <processing dir>/sketchbook/<project
name>/data/
– Use this code:
PImage im = loadImage(“<image filename>”);
June 4, 2010 IAT 334 22
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, 2010 IAT 334 23
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, 2010 IAT 334 24
Accessing Pixels
 How do we know which pixel to look for in the
array?
0 1 3
2 4
0
1
2
3
June 4, 2010 IAT 334 25
Accessing Pixels
 How do we know which pixel to look for in the
array?
0 1 2 3 4
0 1 3
2 4
0
1
2
3
0
June 4, 2010 IAT 334 26
Accessing Pixels
 How do we know which pixel to look for in the
array?
0 1 2 3 4 5 6 7 8 9
0 1 3
2 4
0
1
2
3
0 1
June 4, 2010 IAT 334 27
Accessing Pixels
 How do we know which pixel to look for in the
array?
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 3
2 4
0
1
2
3
0 1 2 3
June 4, 2010 IAT 334 28
Accessing Pixels
 Array Index
– x + y*width
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 3
2 4
0
1
2
3
0 1 2 3
(4, 0) = 4 + 0*5 = 4
(3, 2) = 3 + 2*5 = 13
June 4, 2010 IAT 334 29
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, 2010 IAT 334 30
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, 2010 IAT 334 31
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, 2010 IAT 334 32
2D 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, 2010 IAT 334 33
2D 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, 2010 IAT 334 34
2D 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, 2010 IAT 334 35
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, 2010 IAT 334 36
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, 2010 IAT 334 37
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, 2010 IAT 334 38
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, 2010 IAT 334 39
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, 2010 IAT 334 40
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[365 + 210 * im.width ] ;
loadPixels();
c1 = pixels[ 3 + 2 * 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 + 100 );
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, 2010 IAT 334 41
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, 2010 IAT 334 42
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, 2010 IAT 334 43
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, 2010 IAT 334 44

More Related Content

Similar to IAT334-Lab02-ArraysPImage.pptx

Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docx
dickonsondorris
 
Gdc09 Minigames
Gdc09 MinigamesGdc09 Minigames
Gdc09 Minigames
Susan Gold
 
Client Side Programming with Applet
Client Side Programming with AppletClient Side Programming with Applet
Client Side Programming with Applet
backdoor
 
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
Yashpatel821746
 
Please write it in C not python .pdf
Please write it in C not python .pdfPlease write it in C not python .pdf
Please write it in C not python .pdf
abhisheksharmasre
 
Dotnet unit 4
Dotnet unit 4Dotnet unit 4
Dotnet unit 4
007laksh
 

Similar to IAT334-Lab02-ArraysPImage.pptx (20)

Opensource gis development - part 3
Opensource gis development - part 3Opensource gis development - part 3
Opensource gis development - part 3
 
First kinectslides
First kinectslidesFirst kinectslides
First kinectslides
 
cs247 slides
cs247 slidescs247 slides
cs247 slides
 
Matplotlib
MatplotlibMatplotlib
Matplotlib
 
Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docx
 
maXbox starter68 machine learning VI
maXbox starter68 machine learning VImaXbox starter68 machine learning VI
maXbox starter68 machine learning VI
 
Gdc09 Minigames
Gdc09 MinigamesGdc09 Minigames
Gdc09 Minigames
 
Geospatial Data Analysis and Visualization in Python
Geospatial Data Analysis and Visualization in PythonGeospatial Data Analysis and Visualization in Python
Geospatial Data Analysis and Visualization in Python
 
CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++
 
Op ps
Op psOp ps
Op ps
 
SimpleArray between Python and C++
SimpleArray between Python and C++SimpleArray between Python and C++
SimpleArray between Python and C++
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Client Side Programming with Applet
Client Side Programming with AppletClient Side Programming with Applet
Client Side Programming with Applet
 
Oops recap
Oops recapOops recap
Oops recap
 
Seeing Like Software
Seeing Like SoftwareSeeing Like Software
Seeing Like Software
 
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
 
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
 
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
 
Please write it in C not python .pdf
Please write it in C not python .pdfPlease write it in C not python .pdf
Please write it in C not python .pdf
 
Dotnet unit 4
Dotnet unit 4Dotnet unit 4
Dotnet unit 4
 

More from ssuseraae9cd (10)

IAT334-Lec07-Pen.pptx
IAT334-Lec07-Pen.pptxIAT334-Lec07-Pen.pptx
IAT334-Lec07-Pen.pptx
 
IAT334-Lec10-Rollup.pptx
IAT334-Lec10-Rollup.pptxIAT334-Lec10-Rollup.pptx
IAT334-Lec10-Rollup.pptx
 
IAT334-Lec01-Intro.pptx
IAT334-Lec01-Intro.pptxIAT334-Lec01-Intro.pptx
IAT334-Lec01-Intro.pptx
 
IAT334-Lec02-TaskAnalysis.pptx
IAT334-Lec02-TaskAnalysis.pptxIAT334-Lec02-TaskAnalysis.pptx
IAT334-Lec02-TaskAnalysis.pptx
 
IAT334-Lec03-Cog+UsabilityPrinciples.pptx
IAT334-Lec03-Cog+UsabilityPrinciples.pptxIAT334-Lec03-Cog+UsabilityPrinciples.pptx
IAT334-Lec03-Cog+UsabilityPrinciples.pptx
 
IAT334-Lec05-Dialog.pptx
IAT334-Lec05-Dialog.pptxIAT334-Lec05-Dialog.pptx
IAT334-Lec05-Dialog.pptx
 
IAT334-Lec04-DesignIdeasPrinciples.pptx
IAT334-Lec04-DesignIdeasPrinciples.pptxIAT334-Lec04-DesignIdeasPrinciples.pptx
IAT334-Lec04-DesignIdeasPrinciples.pptx
 
IAT334-Lec09-Errors+Doc.pptx
IAT334-Lec09-Errors+Doc.pptxIAT334-Lec09-Errors+Doc.pptx
IAT334-Lec09-Errors+Doc.pptx
 
IAT334-Lec08-Experiment.pptx
IAT334-Lec08-Experiment.pptxIAT334-Lec08-Experiment.pptx
IAT334-Lec08-Experiment.pptx
 
IAT334-Lec07-Models.pptx
IAT334-Lec07-Models.pptxIAT334-Lec07-Models.pptx
IAT334-Lec07-Models.pptx
 

Recently uploaded

一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理
F
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
ydyuyu
 
一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理
F
 
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Monica Sydney
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Monica Sydney
 
Independent Escorts & Call Girls In Aerocity Delhi - 9758998899 - Escortgram ...
Independent Escorts & Call Girls In Aerocity Delhi - 9758998899 - Escortgram ...Independent Escorts & Call Girls In Aerocity Delhi - 9758998899 - Escortgram ...
Independent Escorts & Call Girls In Aerocity Delhi - 9758998899 - Escortgram ...
Escortgram India
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Monica Sydney
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
ayvbos
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Monica Sydney
 

Recently uploaded (20)

一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理
 
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrStory Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
 
Local Call Girls in Jharsuguda 9332606886 HOT & SEXY Models beautiful and ch...
Local Call Girls in Jharsuguda  9332606886 HOT & SEXY Models beautiful and ch...Local Call Girls in Jharsuguda  9332606886 HOT & SEXY Models beautiful and ch...
Local Call Girls in Jharsuguda 9332606886 HOT & SEXY Models beautiful and ch...
 
一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
 
Independent Escorts & Call Girls In Aerocity Delhi - 9758998899 - Escortgram ...
Independent Escorts & Call Girls In Aerocity Delhi - 9758998899 - Escortgram ...Independent Escorts & Call Girls In Aerocity Delhi - 9758998899 - Escortgram ...
Independent Escorts & Call Girls In Aerocity Delhi - 9758998899 - Escortgram ...
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
 
Research Assignment - NIST SP800 [172 A] - Presentation.pptx
Research Assignment - NIST SP800 [172 A] - Presentation.pptxResearch Assignment - NIST SP800 [172 A] - Presentation.pptx
Research Assignment - NIST SP800 [172 A] - Presentation.pptx
 
South Bopal [ (Call Girls) in Ahmedabad ₹7.5k Pick Up & Drop With Cash Paymen...
South Bopal [ (Call Girls) in Ahmedabad ₹7.5k Pick Up & Drop With Cash Paymen...South Bopal [ (Call Girls) in Ahmedabad ₹7.5k Pick Up & Drop With Cash Paymen...
South Bopal [ (Call Girls) in Ahmedabad ₹7.5k Pick Up & Drop With Cash Paymen...
 
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime BalliaBallia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
 
Dubai Call Girls First Class O525547819 Call Girls Dubai Hot New Girlfriend
Dubai Call Girls First Class O525547819 Call Girls Dubai Hot New GirlfriendDubai Call Girls First Class O525547819 Call Girls Dubai Hot New Girlfriend
Dubai Call Girls First Class O525547819 Call Girls Dubai Hot New Girlfriend
 

IAT334-Lab02-ArraysPImage.pptx

  • 1. IAT 334 Lab 2 Computer Graphics: Rocket, PImage
  • 2. June 4, 2010 IAT 334 2 Outline  Programming concepts – Programming Computer Graphics – Transformations – Methods – Classes – PImage – PFont
  • 3. Arrays  An array is a contiguous collection of data items of one type  Allows you to structure data – Accessed by index number May 21, 2010 IAT 334 3
  • 4. Effect of creating an int variable May 21, 2010 IAT 334 4 // Single int int anInt; // Put a value in the int anInt = 3; // Type error! anInt = “hello”; Code Effect Name: anInt, Type: int 3 Name: anInt, Type: int Name: anInt, Type: int “hello” Can’t shove “hello” into an int
  • 5. Creating an array of ints May 21, 2010 IAT 334 5 // declare int array int[] intArray; // initialize int array intArray = new int[5]; // set first element intArray[0] = 3; // set third element intArray[2] = 5; Code Effect Name: intArray, Type: int[] 0 1 2 3 4 each element has type int 0 0 0 0 0 0 1 2 3 4 3 0 0 0 0 0 1 2 3 4 3 0 5 0 0
  • 6. June 4, 2010 IAT 334 6 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);
  • 7. June 4, 2010 IAT 334 7 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
  • 8. June 4, 2010 IAT 334 8 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)
  • 9. June 4, 2010 IAT 334 9 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)
  • 10. June 4, 2010 IAT 334 10 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
  • 11. June 4, 2010 IAT 334 11 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(); }
  • 12. June 4, 2010 IAT 334 12 Classes
  • 13. June 4, 2010 IAT 334 13 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
  • 14. June 4, 2010 IAT 334 14 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
  • 15. June 4, 2010 IAT 334 15 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; } }
  • 16. June 4, 2010 IAT 334 16 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!)
  • 17. June 4, 2010 IAT 334 17 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!
  • 18. June 4, 2010 IAT 334 18 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();
  • 19. June 4, 2010 IAT 334 19 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();
  • 20. PImage June 4, 2010 IAT 334 20
  • 21. June 4, 2010 IAT 334 21 Loading Images  Loading Images – Give your project a name and save. – Place the image file in: • <processing dir>/sketchbook/<project name>/data/ – Use this code: PImage im = loadImage(“<image filename>”);
  • 22. June 4, 2010 IAT 334 22 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.
  • 23. June 4, 2010 IAT 334 23 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.
  • 24. June 4, 2010 IAT 334 24 Accessing Pixels  How do we know which pixel to look for in the array? 0 1 3 2 4 0 1 2 3
  • 25. June 4, 2010 IAT 334 25 Accessing Pixels  How do we know which pixel to look for in the array? 0 1 2 3 4 0 1 3 2 4 0 1 2 3 0
  • 26. June 4, 2010 IAT 334 26 Accessing Pixels  How do we know which pixel to look for in the array? 0 1 2 3 4 5 6 7 8 9 0 1 3 2 4 0 1 2 3 0 1
  • 27. June 4, 2010 IAT 334 27 Accessing Pixels  How do we know which pixel to look for in the array? 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 3 2 4 0 1 2 3 0 1 2 3
  • 28. June 4, 2010 IAT 334 28 Accessing Pixels  Array Index – x + y*width 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 3 2 4 0 1 2 3 0 1 2 3 (4, 0) = 4 + 0*5 = 4 (3, 2) = 3 + 2*5 = 13
  • 29. June 4, 2010 IAT 334 29 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.
  • 30. June 4, 2010 IAT 334 30 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.
  • 31. June 4, 2010 IAT 334 31 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.
  • 32. June 4, 2010 IAT 334 32 2D 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];
  • 33. June 4, 2010 IAT 334 33 2D 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.
  • 34. June 4, 2010 IAT 334 34 2D 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.
  • 35. June 4, 2010 IAT 334 35 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)
  • 36. June 4, 2010 IAT 334 36 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);
  • 37. June 4, 2010 IAT 334 37 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);
  • 38. June 4, 2010 IAT 334 38 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
  • 39. June 4, 2010 IAT 334 39 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!
  • 40. June 4, 2010 IAT 334 40 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[365 + 210 * im.width ] ; loadPixels(); c1 = pixels[ 3 + 2 * 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 + 100 ); popMatrix(); }
  • 41. 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, 2010 IAT 334 41
  • 42. 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, 2010 IAT 334 42
  • 43. 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, 2010 IAT 334 43
  • 44. 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, 2010 IAT 334 44