jeanine
YaBB Newbies
Offline
Posts: 4
Re: Creating instances of a
Reply #4 - Jun 8th , 2008, 5:46pm
I also am a beginner, though I'm trying to construct examples and lessons for use in a class in the Fall. I came up with the following example. The display screen is blank. When you click the mouse, a flower appears. The color and the number of petals are randomly assigned. void setup() { size (600,400); background(255); } void draw () { } void mousePressed() { int nc = int(random(5)); fill(random(255),random(255),random(255)); int np = int(random(3,8)); Flower f = new Flower(mouseX,mouseY,40,50,np); } class Flower { Flower (float sx, float sy, float wdx, float wdy, int npetals) { translate(sx,sy); for (int p=0; p<=npetals; p++) { rotate (2*PI/npetals); beginShape(); vertex(0,0); bezierVertex(wdx,-wdy, 2*wdx, 3*wdy, 3,3); endShape(); } translate(-sx,-sy); } } Here is a variant: press down, drag and release mouse to set the shapes of the petals. int firstmousex; int firstmousey; void setup() { size (600,400); background(255); } void draw () { } void mousePressed() { firstmousex = mouseX; firstmousey = mouseY; } void mouseReleased() { int nc = int(random(5)); //more red and blue, less green fill(random(200,255),random(100),random(100,200)); int np = int(random(3,8)); int wdx = mouseX-firstmousex; int wdy = mouseY-firstmousey; Flower f = new Flower(firstmousex,firstmousey,wdx,wdy,np); } class Flower { Flower (float sx, float sy, float wdx, float wdy, int npetals) { translate(sx,sy); for (int p=0; p<=npetals; p++) { rotate (2*PI/npetals); beginShape(); vertex(0,0); bezierVertex(wdx,-wdy, 2*wdx, 3*wdy, 3,3); endShape(); } translate(-sx,-sy); } } Regarding your example, I didn't know you could draw within a class. (I had to do this a couple of times to get rid of the smily face. Sorry.)