Array List Balls
in
Programming Questions
•
1 years ago
I used the array list balls tutorial on processing.org but since I'm new to processing I had quite som difficulties.
I managed to change the colors off the ellipses.
But actually I'd like to use a different color everytime an ellipse is created, choosing between (177, 228,0), (242,227,4), (255,178,0) and (255,134,0) randomly.
Can someone please explain to me how I can do that?
Thank you lots in advance..
This is the code, I'm having at the moment:
ArrayList balls;
int ballWidth = 300;
void setup() {
size(1280, 1024);
smooth();
noStroke();
// Create an empty ArrayList
balls = new ArrayList();
// Start by adding one element
balls.add(new Ball(width/2, 0, ballWidth));
}
void draw() {
background(0,0);
// With an array, we say balls.length, with an ArrayList, we say balls.size()
// The length of an ArrayList is dynamic
// Notice how we are looping through the ArrayList backwards
// This is because we are deleting elements from the list
for (int i = balls.size()-1; i >= 0; i--) {
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
Ball ball = (Ball) balls.get(i);
ball.move();
ball.display();
if (ball.finished()) {
// Items can be deleted with remove()
balls.remove(i);
}
}
}
void mousePressed() {
// A new ball object is added to the ArrayList (by default to the end)
balls.add(new Ball(mouseX, mouseY, random(ballWidth)));
}
// Simple bouncing ball class
class Ball {
float x;
float y;
float speed;
float gravity;
float w;
float life = 255;
Ball(float tempX, float tempY, float tempW) {
x = tempX;
y = tempY;
w = tempW;
speed = 0;
gravity = 0.1;
}
void move() {
// Add gravity to speed
speed = speed + gravity;
// Add speed to y location
y = y + speed;
// If square reaches the bottom
// Reverse speed
if (y > height) {
// Dampening
speed = speed * -0.8;
y = height;
}
}
boolean finished() {
// Balls fade out
life--;
if (life < 0) {
return true;
} else {
return false;
}
}
void display()
{
// Display the circle
fill (255, 178, 0, life);
//stroke(0,life);
ellipse(x,y,w,w);
}
I managed to change the colors off the ellipses.
But actually I'd like to use a different color everytime an ellipse is created, choosing between (177, 228,0), (242,227,4), (255,178,0) and (255,134,0) randomly.
Can someone please explain to me how I can do that?
Thank you lots in advance..
This is the code, I'm having at the moment:
ArrayList balls;
int ballWidth = 300;
void setup() {
size(1280, 1024);
smooth();
noStroke();
// Create an empty ArrayList
balls = new ArrayList();
// Start by adding one element
balls.add(new Ball(width/2, 0, ballWidth));
}
void draw() {
background(0,0);
// With an array, we say balls.length, with an ArrayList, we say balls.size()
// The length of an ArrayList is dynamic
// Notice how we are looping through the ArrayList backwards
// This is because we are deleting elements from the list
for (int i = balls.size()-1; i >= 0; i--) {
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
Ball ball = (Ball) balls.get(i);
ball.move();
ball.display();
if (ball.finished()) {
// Items can be deleted with remove()
balls.remove(i);
}
}
}
void mousePressed() {
// A new ball object is added to the ArrayList (by default to the end)
balls.add(new Ball(mouseX, mouseY, random(ballWidth)));
}
// Simple bouncing ball class
class Ball {
float x;
float y;
float speed;
float gravity;
float w;
float life = 255;
Ball(float tempX, float tempY, float tempW) {
x = tempX;
y = tempY;
w = tempW;
speed = 0;
gravity = 0.1;
}
void move() {
// Add gravity to speed
speed = speed + gravity;
// Add speed to y location
y = y + speed;
// If square reaches the bottom
// Reverse speed
if (y > height) {
// Dampening
speed = speed * -0.8;
y = height;
}
}
boolean finished() {
// Balls fade out
life--;
if (life < 0) {
return true;
} else {
return false;
}
}
void display()
{
// Display the circle
fill (255, 178, 0, life);
//stroke(0,life);
ellipse(x,y,w,w);
}
1