/* 6.914, Example 4.B arrays of objects, rectMode, ellipseMode, textAlign, resetMatrix, time functions, filters [...demonstrated by lots of ugly, rectangular-headed bugs!] 0) We create an array of LittleBugs just like we create an array of ints. For each object in the array, we call the constructor with unique parameters. 1) rectMode is by default in CORNER mode, meaning that you specify the position of the rectangle starting with the left upper corner. If you put it in CENTER mode, as we do here, you specify it starting with the center - just like you do with ellipses. 2) ellipseMode (not demonstrated here) is by default in CENTER mode. If you give it CORNER as a parameter instead, you specify the dimensions of the rectangle that the circle fits inside exactly. This is somewhat less useful than being able to pick the center of a rectangle, but it would be useful if you wanted to have a grid of touching ellipses, for instance - each set of four ellipses could have a common starting point, and the same widths and heights, except that some widths and heights would be negative instead of positive. 3) textAlign - which can take parameters LEFT, RIGHT, or CENTER - will let you orient your text around a point. Using CENTER, I can center the text above the bug's head. Its default is LEFT. 4) Your coordinate transformations are literally just ways to multiply a matrix (that is by default an identity matrix) used to plot each of our point on the axes. In order to remove all previous transformations and return to the normal coordinate axis centered at the top left of the screen, use resetMatrix(). 5) In addition to millis(), which measures the time since you've hit play (and the applet has started running), you can use the following functions to get zero-indexed times from your computer: year(), month(), day(), hour(), minute(), second(). For instance, these might be useful to concatenate into a file name if you're saving a series of images and one to remember when they were saved. 6) In the mouseDragged method, you can see the effect that I've set dragging the mouse in different parts of the screen to cause. (Note that the blur effect is very time-intensive.) The four that I've used are: THRESHOLD, INVERT, POSTERIZE and BLUR. There are also two other ones: OPAQUE and GRAY. See the Processing website documentation or use "Reference" in the Help menu to get more information on these. */ PFont myFont; // creating an array of objects int numBugs = 20; LittleBug[] myBugs = new LittleBug[numBugs]; // moving bugs in the array int increment = 20; void setup(){ size(500, 500); myFont = createFont("Tahoma", 16); textFont(myFont, 16); smooth(); colorMode(HSB, 1.0); // initializing array of objects for (int i = 0; i < numBugs; i++){ myBugs[i] = new LittleBug( color(random(.5), 50, 100), // edge color color(random(.5,1), 100, 100),// fill color "bug"+i, // name random(30, 70), // size height/4+(random(width)/2), // xpos height/4+(random(height)/2)); // ypos // uncomment this line to see names myBugs[i].setLabels(true); // applies to labels textAlign(CENTER); // applies to bug heads rectMode(CENTER); } } void draw(){ // delay delay(200); background(0); // move a third of the bugs, randomly chosen for (int i = 0; i < int(numBugs/3); i++){ int tempInt = int(random(numBugs)); // choose a bug myBugs[tempInt].setxPos(myBugs[tempInt].getxPos() + random(-increment, increment)); myBugs[tempInt].setyPos(myBugs[tempInt].getyPos() + random(-increment, increment)); } // draw all bugs for (int i = 0; i < numBugs; i++){ myBugs[i].drawBug(); } } class LittleBug{ color myColor, myEdgeColor; String myName; float mySize, xPos, yPos; boolean labelShowing = false; LittleBug(color c0, color c1, String n, float s, float x, float y){ myColor = c0; myEdgeColor = c1; myName = n; mySize = s; xPos = x; yPos = y; } void drawBug(){ stroke(myEdgeColor); strokeWeight(5); fill(myColor); ellipse(xPos, yPos, mySize, mySize+20); // since rectMode is in CENTER mode, the x-coordinat of the bug's head // being drawn here will be the same as the one at the center of the bug. // (This is contigent on ellipseMode also being CENTER, of course.) rect(xPos, yPos - mySize/2 - 10, mySize/2+5, 20); // since textAlign is in CENTER mode, the x-coordinate will be the same // x-coordinate as the center of the bug. if (labelShowing){ fill(1, 0, 1); text(myName, xPos, yPos - mySize/2 - 30); } } void setBugSize(float newSize){ mySize = newSize; } void setLabels(boolean newSetting){ labelShowing = newSetting; } float getxPos(){ return xPos; } float getyPos(){ return yPos; } void setxPos(float newxPos){ xPos = newxPos; } void setyPos(float newyPos){ yPos = newyPos; } } void mouseDragged(){ if (mouseX < width/2){ if (mouseY < height/2) filter(THRESHOLD); else filter(INVERT); } else { if (mouseY < height/2) filter(POSTERIZE, 2); else filter(BLUR, 3); } }