Classes in Processing
Processing is based on Java, and like Java, it allows you to program in an object-oriented way.
Object-Oriented Programming programs make use of reusable components called Objects, which contain both data (variables) and actions (functions). The main program can create and control objects without needing to know how the object works ‘under the hood’. Instead, calling code just needs to know what an object can do (what functions it has).
| What to Do | What to Know |
|---|---|
| operators | literals |
| expressions | variables |
| statements | arrays |
| flow control | structs |
| functions |
| What to Do + Know |
|---|
| Classes |
| Objects |
Before today, we had not explored defining our own objects in processing. We HAVE used objects, however. A Processing application itself is an object, though processing hides some of the syntax related to that. A PImage is an object as well.
To create your own types of objects, you must write a Class. Classes define what an object will know (its data fields, or variables) and what it can do (its methods, or functions). All object of a given Class, will have the same variables and functions, but the values of each instance can be different. For example, every person has a height but different people can have different heights.
The relationship between classes and objects is important to understand. A Class is defined in code, it is a description of a type of object. Objects are created and only exist while your program runs.
Lets look at an example that uses classes.
// Particle Demo
// Justin Bakse
// PUDT1203 Creative Computing
// This demo creates a basic particle effect. Demonstrates classes and Arraylist
// "particles" will hold all the objects we create
ArrayList particles;
void setup() {
size(600, 400);
ellipseMode(RADIUS);
frameRate(30);
// create 200 DemoParticle instances (objects) and store them in the particles ArrayList
particles = new ArrayList();
for (int p = 0; p < 200; p++) {
DemoParticle particle = new DemoParticle();
particles.add(particle);
}
}
void draw() {
background(255, 153, 0);
noStroke();
fill(50);
// use a loop to access every DemoParticle instance in the ArrayList
// tell each particle to move and draw itself
for (int i = 0; i < particles.size(); i++) {
DemoParticle particle = (DemoParticle) particles.get(i);
particle.moveSelf();
particle.drawSelf();
}
}
// DemoParticle
// Models a simple particle. Particles are created at them mouse location and choose a random speed and direction to travel
class DemoParticle
{
float x;
float y;
float deltaX;
float deltaY;
float radius;
DemoParticle() {
x = mouseX;
y = mouseY;
deltaX = random(-5, 5);
deltaY = random(-5, 5);
radius = random(5, 10);
}
void moveSelf() {
x += deltaX;
y += deltaY;
//reset the position if the particle moves off screen
if (x < 0 || x > width || y < 0 || y > height) {
x = mouseX;
y = mouseY;
}
}
void drawSelf() {
ellipse(x, y, radius, radius);
}
}
This is just an introduction, there is a lot more to learn about Classes and Objects including composition, inheritance, access levels, and passing object values.
Info about Classes, Objects and OOP in Processing:
- Getting Started Book, Chapter 9
- Processing.org Learning OOP
- Processing.org Class Reference
Documenting your Work
Screengrabs in OS X
| Command-Shift-3 | Grab whole screen, save to desktop |
|---|---|
| Command-Shift-4 | Grab selected area, save to desktop |
| Command-Shift-4 then Space | Grab window, save to desktop |
| Command-Control-Shift-3 | Grab whole screen, save to clipboard for pasting |
| Command-Control-Shift-4 | Grab selected area, save to clipboard for pasting |
| Command-Control-Shift-4 then Space | Grab window, save to clipboard for pasting |
Use Applictions/Utilities/Grab.app to take time-delayed screengrabs.
Screencasts in OS X
Use Applications/Quicktime Player to record the screen. Choose File->New Screen Recording. Quicktime Player is not great at this and can take choppy captures. Here are some tips to get it working better.
- Use a fast computer
- Quit every application you can, run just your project and Quicktime Player
- Export an Application (so you can run it without processing, keeping total apps open to a minimum)
- If you have two monitors, disconnect one and work on the smaller one.
- Use a non-retina mac
Several dedicated screencast applications exist for OS X. They tend to work better than Quicktime Player, for a price. Screenflow, Screenflick, and Screenium are all pretty decent. If Quicktime Player isn’t working for you for the assignment, try the demo of one of these. They will watermark your file, but I won’t mind the watermark for this assignment.
Assignments
1. Create Final Midterm Post
Create a post documenting your midterm project, it must include the following:
A short written description about your work.
- 200 to 400 words.
Images of your project.
- Exactly 2 images
- These images should be exactly 600px wide.
- If you did the narrative assignment, use screengrabs of the best two scenes.
- If you created a tool, use one screengrab and one photo of the “specimen”.
- Use Photoshop’s Save for Web feature to prepare the image. Each image should be under 200k.
- Include a short caption in the blog post for each image.
Video of your project being used.
- Exactly 1 video
- Your video should be less than 15 seconds long.
- Your video should be less than 7 MB.
You have been posting your assignments to the class blog all semester. This week, the blog post IS the assignment. Take some time to format your blog post, and take care to make it look good. Once you have made your post, read over the list of requirements above to make sure you did not miss any.
2. Get Materials for Arduino Unit
Next week we will begin the Arduino unit and learn about creating physical interactive projects.
Order the Arduino Budget Pack ($49.95) from Adafruit. This is a basic kit of what you will need for this part of the class. If you think you’ll want to explore further on your own, you might want to get the Arduino Experimentation Kit ($85.00) instead. It has more parts and includes a book with a few introductory circuits. Order your pack right away you will need it for the next class.
Either pack will let you follow along with these tutorials. Take a look at them before next class.
Also take a look at the Arduino site, it has info on installing the Arduino development environment, lessons, tutorials, and reference material.