Slides for Lecture 9

Download Report

Transcript Slides for Lecture 9

IAT 800
ArrayList, Text, and more Image
Manipulation
Oct 17, Fall 2006
IAT 800
Topics
 ArrayLists
– ArrayLists + Polygons
 Some
Oct 17, Fall 2006
words on Strings
IAT 800
ArrayLists

Pros:

Cons:
– Are great if you don’t know how many things are
going to go into the array.
– Automatically resizes when you add to a full
ArrayList.
– Only stores objects, not primitive variables (int, float…).
Need to make objects from those.
– Only returns things of the Object type. Need to
explicitly cast back to the type you put in there.
Oct 17, Fall 2006
IAT 800
ArrayLists

Remember, an ArrayList is just like an array:
If we decide we need to add another element to the array, we need to make a
NEW, bigger array, and copy all the elements into it.
What a pain!
Oct 17, Fall 2006
IAT 800
ArrayLists

ArrayLists do this for us! We don’t even have to
declare a size when we first create it, only
deciding how many elements we’ll have when
we actually add them.
–
–
–
–
ArrayList a = new ArrayList();
a.add(Object o) – adds an object at next index.
a.get(int i) – returns object at i index.
a.size() – returns number of indices in the ArrayList.
Oct 17, Fall 2006
IAT 800
Simple Example - ArrayList
 Let’s
make a little class called Point, which
simply stores an X and Y value associated
with a point on the screen.
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Oct 17, Fall 2006
IAT 800
Simple Example - ArrayList
 ArrayList
of Points instead of two arrays
of x and y points.
ArrayList pointList = new ArrayList();
pointList.add(new Point(45, 50));
pointList.add(new Point(79, 23));
// let’s draw a circle at our second point.
Point p2 = (Point)pointList.get(1);
ellipse(p2.x, p2.y, 50, 50);
Oct 17, Fall 2006
IAT 800
Simple Example - ArrayList
 We
could extend this so that every time
we click, that point gets added to our
ArrayList.
void mouseReleased() {
pointList.add(new Point(mouseX, mouseY));
}
Oct 17, Fall 2006
IAT 800
Simple Example - ArrayList
 We
could then do anything with these
points as they accumulate. Here’s an
example:
Every time we click, the
polygon will re-draw, using
the new point we’ve added.
Oct 17, Fall 2006
void draw() {
background(255);
if(pointList.size() > 1) {
beginShape(POLYGON);
for(int i = 0; i < pointList.size(); i++) {
Point p1 = (Point)pointList.get(i);
vertex(p1.x, p1.y);
}
endShape();
}
}
IAT 800
Types

You may recall when we talked about types
– Primitives
• int, float, byte
• boolean
• char
– Objects (composites)
•
•
•
•
•
Array
ArrayList
PImage
(any object you create)
Strings
Oct 17, Fall 2006
IAT 800
String details
A
string is almost like an array of chars
– char someletter = 'b';
– String somewords = "Howdy-do, mr. jones?";
– Note the use of double-quotes (vs. apostrophes)
 Like
the objects we've created with
classes, it has several methods, too…
Oct 17, Fall 2006
IAT 800
String methods

From
http://processing.org/reference/String.html
– length()
• returns the size of the String (number of letters)
– charAt(number)
• returns the char at an index number
– toUpperCase() and toLowerCase()
• returns a copy of the String in UPPERCASE or lowercase
respectively.
– substring(beginIndex, endIndex)
• returns a portion of the String from beginIndex
String howdy = "Hello!";
Oct 17, Fall 2006
String expletive = howdy.substring(0,3);
IAT 800
String concatenation



Concatenation is just a fancy word for slapping together
to making one!
With Strings, this is done using the + symbol
So, if you have:
String s1 = "She is the ";
String s2 = "programmer."
String sentence = s1 + "awesomest " + s2;

You'll get out:
println(sentence); // sentence == "She is the awesomest programmer."
// outputs: She is the awesomest programmer.
Oct 17, Fall 2006
IAT 800
MORE String
concatenation

You can also add in numbers, too!
String anothersentence = s1 + "#"+ 2 + " " + s2;
// "She is the #2 programmer."

There is also a function called nf() which can
format your numbers (it stands for number
format)
anothersentence = s1 + nf(7,3) + " " + s2;
// nf( integer, number of digits )
// "She is the 007 programmer."
anothersentence = s1 + nf(3.14159,3,2) + " " + s2;
// nf( float, digits before decimal, digits after decimal )
// "She is the 003.14 programmer."

It has siblings! nfs(); nfp(); nfc(); Consult the
reference.
Oct 17, Fall 2006
IAT 800
Strings and Arrays
 Did
you know that you can take an Array
of Strings and join it into one String?
String[] a = { "One", "string", "to", "rule", "them", "all…" };
String tolkien = join(a, " ");
// tolkien == "One string to rule them all…"
 Did
you also know that you can split a
String into an Array?
String b = "Another string to bind them…"
String[] tolkien2= split(b, " ");
// tolkien2 == { "Another", "string", "to", "bind", "them…" }
Oct 17, Fall 2006
IAT 800
Special characters
 Split
based on spaces (" ")
– tab: "\t"
– new line: "\n"
( \ tells the computer to look to the
next character to figure out what to
do that's special.)
String twolines = "I am on one line.\n I am \ton another."
I am on one line.
I am on
another.
– other escape characters include "\\" "\""
Oct 17, Fall 2006
IAT 800
We started with Processing
in…
// any code here, no methods
// methods!
line(0,0,20,20); // …with classes
// global vars
// …and subclasses!
int a;
// (all of the above and then)
// (ALL
class Emotion
{ of the above, and…)
// methods
void setup(){
class Happy extends Emotion {
//fields
}
//new fields
//constructor
void draw(){
//methods //constructor
}
Oct 17, Fall 2006
}
//methods
IAT 800
}
Processing is actually a
Java Class
// Java-Mode!!!
class Uneasy extends PApplet {
// void setup() and void draw() as normally …
//methods
//classes and subclasses
}
Oct 17, Fall 2006
IAT 800
Java Mode

Allows you to program in pure Java
– Can import classes that aren’t normally imported into a
Processing app
– Importing means making a classes available to your program –
the Java API docs tell you where classes are

In Java mode, create a class that extends PApplet
– Normally, all Processing applets extend PApplet behind the scenes

setup(), draw(), etc. are methods of the class extending
PApplet
Oct 17, Fall 2006
IAT 800
A Java-mode program
class MyProgram extends PApplet {
void setup() { … }
void draw() { … }
void myTopLevelMethod() { … }
}
class Text { // Text is just an example
int xPos, yPos;
String word;
…
}
Notice that any classes you define are inside the top class
Oct 17, Fall 2006
IAT 800
Why use Java-mode?

Java-mode gives you access to the entire Java SDK
– We need access to some SDK classes for HTML parsing that
Processing doesn’t make visible by default

Java-mode helps you to understand how Processing is
built on-top of Java
– All those “magic” functions and variables are just methods and
fields of PApplet that your program inherits
Oct 17, Fall 2006
IAT 800
Libraries!

Libraries are other classes (in .java or .jar files )
– Use import nameoflibrary.nameofmethod;
(e.g., import video.*; )

Now with Java-mode, you can ALSO put your
programs in multiple files
– A file for each class
– Create new tabs (files) with that button in the upper
right
Oct 17, Fall 2006
IAT 800
Recap
Strings
 Methods and
concatenation
 Strings and Arrays

Oct 17, Fall 2006
IAT 800