Download presentation
Presentation is loading. Please wait.
1
Computation as an Expressive Medium
Lab 5: ArrayList, Text, and more Image Manipulation Jason Alderman
2
Goooooooooooood Morning
Go over ArrayLists again. (You will love them. You will.) ArrayLists + Polygons = PURE FUN. More Image stuff. Some words on Strings (Not today, unless you really want it.) Extra time for Project 2 and one-on-one questions.
3
First, a clarification…
Yesterday I was a little unsure of the slides: I was wrong! (The slides were RIGHT of course!) Legal statement: Rocket r3 = new ArmedRocket(50, 60, 0); This is CORRECT because ArmedRocket can fulfill all the functionality of Rocket (and then some!) But this is illegal ArmedRocket r4 = new Rocket(50, 60, 0); This is NOT RIGHT because Rocket does not have all the functionality of ArmedRocket! (What happens if you try to refer to a missile firing? Rocket has no missiles. ERROR! DOES NOT COMPUTE!!)
4
ArrayLists ArrayLists are our friends… but also our worst enemies! (Don’t let that scare you away! Come back!) 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.
5
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!
6
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. * What I said yesterday was INCORRECT: a.get(3) is RIGHT, a[3] is WRONG for an ArrayList.
7
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; }
8
Simple Example - ArrayList
Why do this? Let’s make an ArrayList of Points so we don’t need 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);
9
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)); }
10
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.
11
Left To Do We talked about ArrayLists
Now we’ll look at some examples of image manipulation. Moreover, let’s have time for Project 2 and its inherent questions. (No funny today. Sorry.)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.