Presentation is loading. Please wait.

Presentation is loading. Please wait.

Oct 17, Fall 2006IAT 800 ArrayList, Text, and more Image Manipulation.

Similar presentations


Presentation on theme: "Oct 17, Fall 2006IAT 800 ArrayList, Text, and more Image Manipulation."— Presentation transcript:

1 Oct 17, Fall 2006IAT 800 ArrayList, Text, and more Image Manipulation

2 Oct 17, Fall 2006IAT 800 Topics  ArrayLists –ArrayLists + Polygons  Some words on Strings

3 Oct 17, Fall 2006IAT 800 ArrayLists  Pros: –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.  Cons: –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.

4 Oct 17, Fall 2006IAT 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!

5 Oct 17, Fall 2006IAT 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.

6 Oct 17, Fall 2006IAT 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; }

7 Oct 17, Fall 2006IAT 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);

8 Oct 17, Fall 2006IAT 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)); }

9 Oct 17, Fall 2006IAT 800 Simple Example - ArrayList  We could then do anything with these points as they accumulate. Here’s an example: 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(); } Every time we click, the polygon will re-draw, using the new point we’ve added.

10 Oct 17, Fall 2006IAT 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

11 Oct 17, Fall 2006IAT 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…

12 Oct 17, Fall 2006IAT 800 String methods  From http://processing.org/reference/String.html 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!"; String expletive = howdy.substring(0,3);

13 Oct 17, Fall 2006IAT 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:  You'll get out: String s1 = "She is the "; String s2 = "programmer." String sentence = s1 + "awesomest " + s2; println(sentence); // sentence == "She is the awesomest programmer." // outputs: She is the awesomest programmer.

14 Oct 17, Fall 2006IAT 800 MORE String concatenation  You can also add in numbers, too!  There is also a function called nf() which can format your numbers (it stands for number format)  It has siblings! nfs(); nfp(); nfc(); Consult the reference. String anothersentence = s1 + "#"+ 2 + " " + s2; // "She is the #2 programmer." 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."

15 Oct 17, Fall 2006IAT 800 Strings and Arrays  Did you know that you can take an Array of Strings and join it into one String?  Did you also know that you can split a String into an Array? String[] a = { "One", "string", "to", "rule", "them", "all…" }; String tolkien = join(a, " "); // tolkien == "One string to rule them all…" String b = "Another string to bind them…" String[] tolkien2= split(b, " "); // tolkien2 == { "Another", "string", "to", "bind", "them…" }

16 Oct 17, Fall 2006IAT 800 Special characters  Split based on spaces (" ") –tab: "\t" –new line: "\n" –other escape characters include "\\" "\"" String twolines = "I am on one line.\n I am \ton another." I am on one line. I am on another. ( \ tells the computer to look to the next character to figure out what to do that's special.)

17 Oct 17, Fall 2006IAT 800 We started with Processing in… // any code here, no methods line(0,0,20,20); // methods! // global vars int a; // methods void setup(){ } void draw(){ } // …with classes // (all of the above and then) class Emotion { //fields //constructor //methods } // …and subclasses! // (ALL of the above, and…) class Happy extends Emotion { //new fields //constructor //methods }

18 Oct 17, Fall 2006IAT 800 Processing is actually a Java Class // Java-Mode!!! class Uneasy extends PApplet { // void setup() and void draw() as normally … //methods //classes and subclasses }

19 Oct 17, Fall 2006IAT 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

20 Oct 17, Fall 2006IAT 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

21 Oct 17, Fall 2006IAT 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

22 Oct 17, Fall 2006IAT 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

23 Oct 17, Fall 2006IAT 800 Recap  Strings  Methods and concatenation  Strings and Arrays


Download ppt "Oct 17, Fall 2006IAT 800 ArrayList, Text, and more Image Manipulation."

Similar presentations


Ads by Google