// Hi y'all, it's me Nokjao again. Please take a look at my code here:
//// The code starts here ////
GumBall[] balls = new GumBall[100];
void setup() {
size(480, 480);
smooth();
colorMode(RGB);
randomSeed(2);
for (int i = 0; i < balls.length; i++) {
float x = random(width);
float y = random(height);
float d = random(10, width/3);
color c = color(random(0, 255), random(40, 255), random(70, 255), random(30, 100));
balls[i] = new GumBall(x, y, d, c);
}
}
void draw() {
background(255);
for (int i = 0; i < balls.length; i++) {
balls[i].display();
balls[i].jitter();
balls[i].scatter();
}
}
//// class GumBall ////
class GumBall {
float x;
float y;
float d;
float homeX;
float homeY;
color c;
float linger = 0.4;
float speed = 3;
Fur furs = new Fur(x, y, d, c);
GumBall(float xi, float yi, float di, color ci) {
x = homeX = xi;
y = homeY = yi;
d = di;
c = ci;
furs.start();
}
void display() {
noStroke();
fill(c);
ellipseMode(CENTER);
ellipse(x, y, d, d);
fill(alpha(c));
ellipse(x, y, d/75, d/75);
}
void jitter() {
if ((mouseX < x+100) && (mouseX > x-100)) {
if ((mouseY < y+100) && (mouseY > y-100)) {
x += (noise(-speed, speed))*3;
y += (noise(-speed, speed))*3;
}
}
else {
x += random(-linger, linger);
y += random(-linger, linger);
}
}
void scatter() {
float easing = 0.05;
float targetX = x;
float targetY = y;
if (mousePressed) {
x += (targetX - (random(-d, width+d))) * easing;
y += (targetY - (random(-d, height+d))) * easing;
}
else {
float easingBack = 0.08;
x += (homeX - x) * easingBack;
y += (homeY - y) * easingBack;
}
}
}
//// subclass Fur, which inherits GumBall ////
class Fur extends GumBall {
// Constructor for Fur class inheriting variables from GumBall superclass
Fur(float x, float y, float d, color c) {
super(x, y, d, c);
}
void start() {
strokeWeight(0);
stroke(alpha(c));
pushMatrix();
translate(x, y);
for (int deg = 0; deg < 360; deg += 10) {
line(0, 0, cos(radians(deg)) * (d/2), sin(radians(deg)) * (d/2));
}
popMatrix();
}
}
/*
What I'm trying to do here is to "embed" the Fur objects inside the GumBall objects so that when GumBall objects
are called, the Fur objects are contained inside them and inherit their behaviors (or methods).
I've tried several methods and in one method the processing rendered erratically, while with this code it just did not run at all with the following error message:
"StackOverflowError: This sketch is attempting too many recursions"
Please help me on this, thanks so much.
Nokjao
*/
//// The code starts here ////
GumBall[] balls = new GumBall[100];
void setup() {
size(480, 480);
smooth();
colorMode(RGB);
randomSeed(2);
for (int i = 0; i < balls.length; i++) {
float x = random(width);
float y = random(height);
float d = random(10, width/3);
color c = color(random(0, 255), random(40, 255), random(70, 255), random(30, 100));
balls[i] = new GumBall(x, y, d, c);
}
}
void draw() {
background(255);
for (int i = 0; i < balls.length; i++) {
balls[i].display();
balls[i].jitter();
balls[i].scatter();
}
}
//// class GumBall ////
class GumBall {
float x;
float y;
float d;
float homeX;
float homeY;
color c;
float linger = 0.4;
float speed = 3;
Fur furs = new Fur(x, y, d, c);
GumBall(float xi, float yi, float di, color ci) {
x = homeX = xi;
y = homeY = yi;
d = di;
c = ci;
furs.start();
}
void display() {
noStroke();
fill(c);
ellipseMode(CENTER);
ellipse(x, y, d, d);
fill(alpha(c));
ellipse(x, y, d/75, d/75);
}
void jitter() {
if ((mouseX < x+100) && (mouseX > x-100)) {
if ((mouseY < y+100) && (mouseY > y-100)) {
x += (noise(-speed, speed))*3;
y += (noise(-speed, speed))*3;
}
}
else {
x += random(-linger, linger);
y += random(-linger, linger);
}
}
void scatter() {
float easing = 0.05;
float targetX = x;
float targetY = y;
if (mousePressed) {
x += (targetX - (random(-d, width+d))) * easing;
y += (targetY - (random(-d, height+d))) * easing;
}
else {
float easingBack = 0.08;
x += (homeX - x) * easingBack;
y += (homeY - y) * easingBack;
}
}
}
//// subclass Fur, which inherits GumBall ////
class Fur extends GumBall {
// Constructor for Fur class inheriting variables from GumBall superclass
Fur(float x, float y, float d, color c) {
super(x, y, d, c);
}
void start() {
strokeWeight(0);
stroke(alpha(c));
pushMatrix();
translate(x, y);
for (int deg = 0; deg < 360; deg += 10) {
line(0, 0, cos(radians(deg)) * (d/2), sin(radians(deg)) * (d/2));
}
popMatrix();
}
}
/*
What I'm trying to do here is to "embed" the Fur objects inside the GumBall objects so that when GumBall objects
are called, the Fur objects are contained inside them and inherit their behaviors (or methods).
I've tried several methods and in one method the processing rendered erratically, while with this code it just did not run at all with the following error message:
"StackOverflowError: This sketch is attempting too many recursions"
Please help me on this, thanks so much.
Nokjao
*/
1